2014-10-08 02:19:55 +04:00
|
|
|
require 'jasmine-json'
|
2015-10-17 02:49:24 +03:00
|
|
|
require '../src/window'
|
2013-09-17 22:33:56 +04:00
|
|
|
require '../vendor/jasmine-jquery'
|
2013-09-04 04:01:35 +04:00
|
|
|
path = require 'path'
|
2014-02-24 05:09:05 +04:00
|
|
|
_ = require 'underscore-plus'
|
|
|
|
fs = require 'fs-plus'
|
2014-04-22 21:11:52 +04:00
|
|
|
Grim = require 'grim'
|
2015-10-09 23:46:51 +03:00
|
|
|
pathwatcher = require 'pathwatcher'
|
2015-10-15 01:35:00 +03:00
|
|
|
FindParentDir = require 'find-parent-dir'
|
2014-11-19 04:48:28 +03:00
|
|
|
|
2014-09-23 01:35:13 +04:00
|
|
|
TextEditor = require '../src/text-editor'
|
2014-11-27 03:51:13 +03:00
|
|
|
TextEditorElement = require '../src/text-editor-element'
|
2013-09-18 05:58:41 +04:00
|
|
|
TokenizedBuffer = require '../src/tokenized-buffer'
|
2015-04-14 20:34:43 +03:00
|
|
|
clipboard = require '../src/safe-clipboard'
|
2013-09-04 23:38:20 +04:00
|
|
|
|
2015-10-07 00:12:56 +03:00
|
|
|
jasmineStyle = document.createElement('style')
|
|
|
|
jasmineStyle.textContent = atom.themes.loadStylesheet(atom.themes.resolveStylesheet('../static/jasmine'))
|
|
|
|
document.head.appendChild(jasmineStyle)
|
|
|
|
|
2013-09-18 01:54:33 +04:00
|
|
|
fixturePackagesPath = path.resolve(__dirname, './fixtures/packages')
|
2013-10-08 01:52:30 +04:00
|
|
|
atom.packages.packageDirPaths.unshift(fixturePackagesPath)
|
2012-12-19 06:47:20 +04:00
|
|
|
|
2015-09-04 19:21:00 +03:00
|
|
|
document.querySelector('html').style.overflow = 'auto'
|
|
|
|
document.body.style.overflow = 'auto'
|
2013-02-20 23:44:50 +04:00
|
|
|
|
2015-05-30 03:18:48 +03:00
|
|
|
Set.prototype.jasmineToString = ->
|
|
|
|
result = "Set {"
|
|
|
|
first = true
|
|
|
|
@forEach (element) ->
|
|
|
|
result += ", " unless first
|
|
|
|
result += element.toString()
|
|
|
|
first = false
|
|
|
|
result + "}"
|
|
|
|
|
|
|
|
Set.prototype.isEqual = (other) ->
|
|
|
|
if other instanceof Set
|
|
|
|
return false if @size isnt other.size
|
|
|
|
values = @values()
|
|
|
|
until (next = values.next()).done
|
|
|
|
return false unless other.has(next.value)
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
|
2013-02-20 23:44:50 +04:00
|
|
|
jasmine.getEnv().addEqualityTester(_.isEqual) # Use underscore's definition of equality for toEqual assertions
|
2014-06-18 21:04:07 +04:00
|
|
|
|
2015-04-16 10:15:46 +03:00
|
|
|
if process.env.CI
|
2014-07-07 21:14:38 +04:00
|
|
|
jasmine.getEnv().defaultTimeoutInterval = 60000
|
2014-06-18 21:04:07 +04:00
|
|
|
else
|
|
|
|
jasmine.getEnv().defaultTimeoutInterval = 5000
|
2013-02-20 23:44:50 +04:00
|
|
|
|
2016-09-26 20:52:13 +03:00
|
|
|
{testPaths} = atom.getLoadSettings()
|
2015-10-15 01:35:00 +03:00
|
|
|
|
|
|
|
if specPackagePath = FindParentDir.sync(testPaths[0], 'package.json')
|
|
|
|
packageMetadata = require(path.join(specPackagePath, 'package.json'))
|
|
|
|
specPackageName = packageMetadata.name
|
|
|
|
|
|
|
|
if specDirectory = FindParentDir.sync(testPaths[0], 'fixtures')
|
|
|
|
specProjectPath = path.join(specDirectory, 'fixtures')
|
2015-10-15 02:10:30 +03:00
|
|
|
else
|
|
|
|
specProjectPath = path.join(__dirname, 'fixtures')
|
2013-11-22 02:55:25 +04:00
|
|
|
|
2012-08-28 02:36:36 +04:00
|
|
|
beforeEach ->
|
2015-10-15 01:35:00 +03:00
|
|
|
atom.project.setPaths([specProjectPath])
|
2015-10-09 03:54:43 +03:00
|
|
|
|
2012-08-28 02:36:36 +04:00
|
|
|
window.resetTimeouts()
|
2014-12-30 10:52:16 +03:00
|
|
|
spyOn(_._, "now").andCallFake -> window.now
|
2017-07-31 19:14:44 +03:00
|
|
|
spyOn(Date, 'now').andCallFake(-> window.now)
|
2014-12-30 10:52:16 +03:00
|
|
|
spyOn(window, "setTimeout").andCallFake window.fakeSetTimeout
|
|
|
|
spyOn(window, "clearTimeout").andCallFake window.fakeClearTimeout
|
|
|
|
|
2015-10-15 01:35:00 +03:00
|
|
|
spy = spyOn(atom.packages, 'resolvePackagePath').andCallFake (packageName) ->
|
|
|
|
if specPackageName and packageName is specPackageName
|
|
|
|
resolvePackagePath(specPackagePath)
|
|
|
|
else
|
|
|
|
resolvePackagePath(packageName)
|
|
|
|
resolvePackagePath = _.bind(spy.originalValue, atom.packages)
|
2013-10-11 00:03:44 +04:00
|
|
|
|
2013-10-09 03:23:34 +04:00
|
|
|
# prevent specs from modifying Atom's menus
|
|
|
|
spyOn(atom.menu, 'sendToBrowserProcess')
|
|
|
|
|
2015-10-14 13:47:33 +03:00
|
|
|
# reset config before each spec
|
2015-09-28 22:21:50 +03:00
|
|
|
atom.config.set "core.destroyEmptyPanes", false
|
|
|
|
atom.config.set "editor.fontFamily", "Courier"
|
|
|
|
atom.config.set "editor.fontSize", 16
|
|
|
|
atom.config.set "editor.autoIndent", false
|
|
|
|
atom.config.set "core.disabledPackages", ["package-that-throws-an-exception",
|
2013-10-11 22:56:12 +04:00
|
|
|
"package-with-broken-package-json", "package-with-broken-keymap"]
|
2014-12-30 10:52:41 +03:00
|
|
|
advanceClock(1000)
|
2015-01-13 20:17:48 +03:00
|
|
|
window.setTimeout.reset()
|
2012-12-13 03:23:36 +04:00
|
|
|
|
2012-11-15 00:13:57 +04:00
|
|
|
# make editor display updates synchronous
|
2014-11-27 03:51:13 +03:00
|
|
|
TextEditorElement::setUpdatedSynchronously(true)
|
2014-06-20 03:31:06 +04:00
|
|
|
|
2014-02-19 22:01:56 +04:00
|
|
|
spyOn(pathwatcher.File.prototype, "detectResurrectionAfterDelay").andCallFake -> @detectResurrection()
|
2014-09-23 01:35:13 +04:00
|
|
|
spyOn(TextEditor.prototype, "shouldPromptToSave").andReturn false
|
2012-11-15 00:13:57 +04:00
|
|
|
|
2012-11-21 19:22:13 +04:00
|
|
|
# make tokenization synchronous
|
|
|
|
TokenizedBuffer.prototype.chunkSize = Infinity
|
|
|
|
spyOn(TokenizedBuffer.prototype, "tokenizeInBackground").andCallFake -> @tokenizeNextChunk()
|
|
|
|
|
2014-02-04 01:12:33 +04:00
|
|
|
clipboardContent = 'initial clipboard content'
|
|
|
|
spyOn(clipboard, 'writeText').andCallFake (text) -> clipboardContent = text
|
|
|
|
spyOn(clipboard, 'readText').andCallFake -> clipboardContent
|
2013-01-10 22:56:55 +04:00
|
|
|
|
2013-03-07 23:04:17 +04:00
|
|
|
addCustomMatchers(this)
|
|
|
|
|
2012-08-28 02:36:36 +04:00
|
|
|
afterEach ->
|
2016-10-04 19:26:37 +03:00
|
|
|
ensureNoDeprecatedFunctionCalls()
|
|
|
|
ensureNoDeprecatedStylesheets()
|
2017-09-06 02:12:12 +03:00
|
|
|
|
|
|
|
waitsForPromise ->
|
|
|
|
atom.reset()
|
|
|
|
|
|
|
|
runs ->
|
|
|
|
document.getElementById('jasmine-content').innerHTML = '' unless window.debugContent
|
|
|
|
warnIfLeakingPathSubscriptions()
|
|
|
|
waits(0) # yield to ui thread to make screen update more frequently
|
2012-11-29 00:40:37 +04:00
|
|
|
|
2016-03-30 01:46:57 +03:00
|
|
|
warnIfLeakingPathSubscriptions = ->
|
2013-04-07 12:18:08 +04:00
|
|
|
watchedPaths = pathwatcher.getWatchedPaths()
|
2012-11-29 04:16:15 +04:00
|
|
|
if watchedPaths.length > 0
|
2016-03-30 01:46:57 +03:00
|
|
|
console.error("WARNING: Leaking subscriptions for paths: " + watchedPaths.join(", "))
|
|
|
|
pathwatcher.closeAllWatchers()
|
2012-08-28 02:36:36 +04:00
|
|
|
|
2016-10-04 19:26:37 +03:00
|
|
|
ensureNoDeprecatedFunctionCalls = ->
|
|
|
|
deprecations = _.clone(Grim.getDeprecations())
|
|
|
|
Grim.clearDeprecations()
|
2014-04-22 21:43:55 +04:00
|
|
|
if deprecations.length > 0
|
|
|
|
originalPrepareStackTrace = Error.prepareStackTrace
|
|
|
|
Error.prepareStackTrace = (error, stack) ->
|
|
|
|
output = []
|
|
|
|
for deprecation in deprecations
|
2014-04-22 22:01:05 +04:00
|
|
|
output.push "#{deprecation.originName} is deprecated. #{deprecation.message}"
|
2014-04-22 21:43:55 +04:00
|
|
|
output.push _.multiplyString("-", output[output.length - 1].length)
|
|
|
|
for stack in deprecation.getStacks()
|
|
|
|
for {functionName, location} in stack
|
|
|
|
output.push "#{functionName} -- #{location}"
|
|
|
|
output.push ""
|
|
|
|
output.join("\n")
|
|
|
|
|
|
|
|
error = new Error("Deprecated function(s) #{deprecations.map(({originName}) -> originName).join ', '}) were called.")
|
|
|
|
error.stack
|
|
|
|
Error.prepareStackTrace = originalPrepareStackTrace
|
|
|
|
throw error
|
|
|
|
|
2016-10-04 19:26:37 +03:00
|
|
|
ensureNoDeprecatedStylesheets = ->
|
|
|
|
deprecations = _.clone(atom.styles.getDeprecations())
|
|
|
|
atom.styles.clearDeprecations()
|
|
|
|
for sourcePath, deprecation of deprecations
|
|
|
|
title =
|
|
|
|
if sourcePath isnt 'undefined'
|
|
|
|
"Deprecated stylesheet at '#{sourcePath}':"
|
|
|
|
else
|
|
|
|
"Deprecated stylesheet:"
|
|
|
|
throw new Error("#{title}\n#{deprecation.message}")
|
|
|
|
|
2012-08-28 02:36:36 +04:00
|
|
|
emitObject = jasmine.StringPrettyPrinter.prototype.emitObject
|
|
|
|
jasmine.StringPrettyPrinter.prototype.emitObject = (obj) ->
|
|
|
|
if obj.inspect
|
|
|
|
@append obj.inspect()
|
|
|
|
else
|
|
|
|
emitObject.call(this, obj)
|
|
|
|
|
2012-11-17 04:12:04 +04:00
|
|
|
jasmine.unspy = (object, methodName) ->
|
2013-12-12 08:37:52 +04:00
|
|
|
throw new Error("Not a spy") unless object[methodName].hasOwnProperty('originalValue')
|
2012-11-17 04:12:04 +04:00
|
|
|
object[methodName] = object[methodName].originalValue
|
|
|
|
|
2014-11-20 03:20:39 +03:00
|
|
|
jasmine.attachToDOM = (element) ->
|
|
|
|
jasmineContent = document.querySelector('#jasmine-content')
|
|
|
|
jasmineContent.appendChild(element) unless jasmineContent.contains(element)
|
|
|
|
|
2016-10-04 19:26:37 +03:00
|
|
|
grimDeprecationsSnapshot = null
|
|
|
|
stylesDeprecationsSnapshot = null
|
2014-11-26 00:51:56 +03:00
|
|
|
jasmine.snapshotDeprecations = ->
|
2016-10-04 19:26:37 +03:00
|
|
|
grimDeprecationsSnapshot = _.clone(Grim.deprecations)
|
|
|
|
stylesDeprecationsSnapshot = _.clone(atom.styles.deprecationsBySourcePath)
|
2014-11-26 00:51:56 +03:00
|
|
|
|
|
|
|
jasmine.restoreDeprecationsSnapshot = ->
|
2016-10-04 19:26:37 +03:00
|
|
|
Grim.deprecations = grimDeprecationsSnapshot
|
|
|
|
atom.styles.deprecationsBySourcePath = stylesDeprecationsSnapshot
|
2014-11-26 00:51:56 +03:00
|
|
|
|
2015-01-23 04:59:18 +03:00
|
|
|
jasmine.useRealClock = ->
|
|
|
|
jasmine.unspy(window, 'setTimeout')
|
|
|
|
jasmine.unspy(window, 'clearTimeout')
|
|
|
|
jasmine.unspy(_._, 'now')
|
2017-07-31 19:14:44 +03:00
|
|
|
jasmine.unspy(Date, 'now')
|
2015-01-23 04:59:18 +03:00
|
|
|
|
2016-08-11 16:55:32 +03:00
|
|
|
# The clock is halfway mocked now in a sad and terrible way... only setTimeout
|
|
|
|
# and clearTimeout are included. This method will also include setInterval. We
|
|
|
|
# would do this everywhere if didn't cause us to break a bunch of package tests.
|
|
|
|
jasmine.useMockClock = ->
|
|
|
|
spyOn(window, 'setInterval').andCallFake(fakeSetInterval)
|
|
|
|
spyOn(window, 'clearInterval').andCallFake(fakeClearInterval)
|
|
|
|
|
2013-03-07 23:04:17 +04:00
|
|
|
addCustomMatchers = (spec) ->
|
|
|
|
spec.addMatchers
|
|
|
|
toBeInstanceOf: (expected) ->
|
2016-04-15 21:25:05 +03:00
|
|
|
beOrNotBe = if @isNot then "not be" else "be"
|
|
|
|
this.message = => "Expected #{jasmine.pp(@actual)} to #{beOrNotBe} instance of #{expected.name} class"
|
2013-03-07 23:04:17 +04:00
|
|
|
@actual instanceof expected
|
|
|
|
|
|
|
|
toHaveLength: (expected) ->
|
2013-09-09 20:28:36 +04:00
|
|
|
if not @actual?
|
|
|
|
this.message = => "Expected object #{@actual} has no length method"
|
|
|
|
false
|
|
|
|
else
|
2016-04-15 21:25:05 +03:00
|
|
|
haveOrNotHave = if @isNot then "not have" else "have"
|
|
|
|
this.message = => "Expected object with length #{@actual.length} to #{haveOrNotHave} length #{expected}"
|
2015-04-07 06:45:02 +03:00
|
|
|
@actual.length is expected
|
2013-03-07 23:04:17 +04:00
|
|
|
|
2013-03-13 03:55:28 +04:00
|
|
|
toExistOnDisk: (expected) ->
|
2016-04-15 21:25:05 +03:00
|
|
|
toOrNotTo = this.isNot and "not to" or "to"
|
|
|
|
@message = -> return "Expected path '#{@actual}' #{toOrNotTo} exist."
|
2013-11-01 00:43:44 +04:00
|
|
|
fs.existsSync(@actual)
|
2013-03-13 03:55:28 +04:00
|
|
|
|
2014-02-05 01:06:47 +04:00
|
|
|
toHaveFocus: ->
|
2016-04-15 21:25:05 +03:00
|
|
|
toOrNotTo = this.isNot and "not to" or "to"
|
2014-02-05 01:06:47 +04:00
|
|
|
if not document.hasFocus()
|
|
|
|
console.error "Specs will fail because the Dev Tools have focus. To fix this close the Dev Tools or click the spec runner."
|
|
|
|
|
2016-04-15 21:25:05 +03:00
|
|
|
@message = -> return "Expected element '#{@actual}' or its descendants #{toOrNotTo} have focus."
|
2014-02-05 01:06:47 +04:00
|
|
|
element = @actual
|
|
|
|
element = element.get(0) if element.jquery
|
2014-10-16 23:53:17 +04:00
|
|
|
element is document.activeElement or element.contains(document.activeElement)
|
2014-02-05 01:06:47 +04:00
|
|
|
|
2014-08-14 00:41:56 +04:00
|
|
|
toShow: ->
|
2016-04-15 21:25:05 +03:00
|
|
|
toOrNotTo = this.isNot and "not to" or "to"
|
2014-08-14 00:41:56 +04:00
|
|
|
element = @actual
|
|
|
|
element = element.get(0) if element.jquery
|
2016-04-15 21:25:05 +03:00
|
|
|
@message = -> return "Expected element '#{element}' or its descendants #{toOrNotTo} show."
|
2014-08-14 00:41:56 +04:00
|
|
|
element.style.display in ['block', 'inline-block', 'static', 'fixed']
|
|
|
|
|
2016-04-20 00:25:44 +03:00
|
|
|
toEqualPath: (expected) ->
|
2016-04-15 21:25:05 +03:00
|
|
|
actualPath = path.normalize(@actual)
|
|
|
|
expectedPath = path.normalize(expected)
|
|
|
|
@message = -> return "Expected path '#{actualPath}' to be equal to '#{expectedPath}'."
|
|
|
|
actualPath is expectedPath
|
|
|
|
|
2012-08-28 02:36:36 +04:00
|
|
|
window.waitsForPromise = (args...) ->
|
2016-01-15 05:17:29 +03:00
|
|
|
label = null
|
2012-08-28 02:36:36 +04:00
|
|
|
if args.length > 1
|
2016-01-15 04:47:02 +03:00
|
|
|
{shouldReject, timeout, label} = args[0]
|
2012-08-28 02:36:36 +04:00
|
|
|
else
|
|
|
|
shouldReject = false
|
2016-01-15 05:17:29 +03:00
|
|
|
label ?= 'promise to be resolved or rejected'
|
2012-08-28 02:36:36 +04:00
|
|
|
fn = _.last(args)
|
|
|
|
|
2016-01-15 04:47:02 +03:00
|
|
|
window.waitsFor label, timeout, (moveOn) ->
|
2012-08-28 02:36:36 +04:00
|
|
|
promise = fn()
|
|
|
|
if shouldReject
|
2015-02-10 01:22:11 +03:00
|
|
|
promise.catch.call(promise, moveOn)
|
2015-01-23 04:26:15 +03:00
|
|
|
promise.then ->
|
2012-08-28 02:36:36 +04:00
|
|
|
jasmine.getEnv().currentSpec.fail("Expected promise to be rejected, but it was resolved")
|
|
|
|
moveOn()
|
|
|
|
else
|
2015-01-23 04:26:15 +03:00
|
|
|
promise.then(moveOn)
|
2015-02-10 01:22:11 +03:00
|
|
|
promise.catch.call promise, (error) ->
|
2015-05-08 21:16:19 +03:00
|
|
|
jasmine.getEnv().currentSpec.fail("Expected promise to be resolved, but it was rejected with: #{error?.message} #{jasmine.pp(error)}")
|
2012-08-28 02:36:36 +04:00
|
|
|
moveOn()
|
|
|
|
|
|
|
|
window.resetTimeouts = ->
|
|
|
|
window.now = 0
|
|
|
|
window.timeoutCount = 0
|
2014-05-21 00:03:44 +04:00
|
|
|
window.intervalCount = 0
|
2012-08-28 02:36:36 +04:00
|
|
|
window.timeouts = []
|
2014-05-21 00:03:44 +04:00
|
|
|
window.intervalTimeouts = {}
|
2012-08-28 02:36:36 +04:00
|
|
|
|
2016-08-11 16:55:32 +03:00
|
|
|
window.fakeSetTimeout = (callback, ms=0) ->
|
2012-08-28 02:36:36 +04:00
|
|
|
id = ++window.timeoutCount
|
|
|
|
window.timeouts.push([id, window.now + ms, callback])
|
|
|
|
id
|
|
|
|
|
2012-11-17 04:13:08 +04:00
|
|
|
window.fakeClearTimeout = (idToClear) ->
|
2015-04-07 06:45:02 +03:00
|
|
|
window.timeouts = window.timeouts.filter ([id]) -> id isnt idToClear
|
2012-08-28 02:36:36 +04:00
|
|
|
|
2014-04-07 23:51:25 +04:00
|
|
|
window.fakeSetInterval = (callback, ms) ->
|
2014-05-21 00:03:44 +04:00
|
|
|
id = ++window.intervalCount
|
2014-04-07 23:51:25 +04:00
|
|
|
action = ->
|
|
|
|
callback()
|
2014-05-21 00:03:44 +04:00
|
|
|
window.intervalTimeouts[id] = window.fakeSetTimeout(action, ms)
|
|
|
|
window.intervalTimeouts[id] = window.fakeSetTimeout(action, ms)
|
|
|
|
id
|
2014-04-07 23:51:25 +04:00
|
|
|
|
|
|
|
window.fakeClearInterval = (idToClear) ->
|
2014-05-21 00:03:44 +04:00
|
|
|
window.fakeClearTimeout(@intervalTimeouts[idToClear])
|
2014-04-07 23:51:25 +04:00
|
|
|
|
2012-08-28 02:36:36 +04:00
|
|
|
window.advanceClock = (delta=1) ->
|
|
|
|
window.now += delta
|
2012-11-20 03:50:07 +04:00
|
|
|
callbacks = []
|
|
|
|
|
2012-08-28 02:36:36 +04:00
|
|
|
window.timeouts = window.timeouts.filter ([id, strikeTime, callback]) ->
|
|
|
|
if strikeTime <= window.now
|
2012-11-20 03:50:07 +04:00
|
|
|
callbacks.push(callback)
|
2012-08-28 02:36:36 +04:00
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
|
2012-11-20 03:50:07 +04:00
|
|
|
callback() for callback in callbacks
|
2015-11-24 04:35:38 +03:00
|
|
|
|
|
|
|
exports.mockLocalStorage = ->
|
|
|
|
items = {}
|
|
|
|
spyOn(global.localStorage, 'setItem').andCallFake (key, item) -> items[key] = item.toString(); undefined
|
|
|
|
spyOn(global.localStorage, 'getItem').andCallFake (key) -> items[key] ? null
|
|
|
|
spyOn(global.localStorage, 'removeItem').andCallFake (key) -> delete items[key]; undefined
|