Skip to content
Permalink
Browse files

Revert: ngAnimate changes for listening on visibilitychange

Reverts d3e123b, bf1acf7 and aa28e48.
Backporting the changes is complicated because we don't destroy the rootScope
after each test in 1.4.x
(#14574)
  • Loading branch information
Narretz committed May 9, 2016
1 parent d3e123b commit b24bfae5857394b57eed0304a1b3bc11400c7f5d
@@ -64,7 +64,6 @@
$CacheFactoryProvider,
$ControllerProvider,
$DocumentProvider,
$$IsDocumentHiddenProvider,
$ExceptionHandlerProvider,
$FilterProvider,
$$ForceReflowProvider,
@@ -227,7 +226,6 @@ function publishExternalAPI(angular) {
$cacheFactory: $CacheFactoryProvider,
$controller: $ControllerProvider,
$document: $DocumentProvider,
$$isDocumentHidden: $$IsDocumentHiddenProvider,
$exceptionHandler: $ExceptionHandlerProvider,
$filter: $FilterProvider,
$$forceReflow: $$ForceReflowProvider,
@@ -28,8 +28,8 @@ var $$AnimateAsyncRunFactoryProvider = function() {
};

var $$AnimateRunnerFactoryProvider = function() {
this.$get = ['$q', '$sniffer', '$$animateAsyncRun', '$$isDocumentHidden', '$timeout',
function($q, $sniffer, $$animateAsyncRun, $$isDocumentHidden, $timeout) {
this.$get = ['$q', '$sniffer', '$$animateAsyncRun', '$document', '$timeout',
function($q, $sniffer, $$animateAsyncRun, $document, $timeout) {

var INITIAL_STATE = 0;
var DONE_PENDING_STATE = 1;
@@ -81,7 +81,11 @@ var $$AnimateRunnerFactoryProvider = function() {

this._doneCallbacks = [];
this._tick = function(fn) {
if ($$isDocumentHidden()) {
var doc = $document[0];

// the document may not be ready or attached
// to the module for some internal tests
if (doc && doc.hidden) {
timeoutTick(fn);
} else {
rafTick(fn);
@@ -30,29 +30,3 @@ function $DocumentProvider() {
return jqLite(window.document);
}];
}


/**
* @private
* Listens for document visibility change and makes the current status accessible.
*/
function $$IsDocumentHiddenProvider() {
this.$get = ['$document', '$rootScope', function($document, $rootScope) {
var doc = $document[0];
var hidden = doc && doc.hidden;

$document.on('visibilitychange', changeListener);

$rootScope.$on('$destroy', function() {
$document.off('visibilitychange', changeListener);
});

function changeListener() {
hidden = doc.hidden;
}

return function() {
return hidden;
};
}];
}
@@ -97,10 +97,8 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {

this.$get = ['$$rAF', '$rootScope', '$rootElement', '$document', '$$HashMap',
'$$animation', '$$AnimateRunner', '$templateRequest', '$$jqLite', '$$forceReflow',
'$$isDocumentHidden',
function($$rAF, $rootScope, $rootElement, $document, $$HashMap,
$$animation, $$AnimateRunner, $templateRequest, $$jqLite, $$forceReflow,
$$isDocumentHidden) {
$$animation, $$AnimateRunner, $templateRequest, $$jqLite, $$forceReflow) {

var activeAnimationsLookup = new $$HashMap();
var disabledElementsLookup = new $$HashMap();
@@ -333,7 +331,7 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {

var isStructural = ['enter', 'move', 'leave'].indexOf(event) >= 0;

var documentHidden = $$isDocumentHidden();
var documentHidden = $document[0].hidden;

// this is a hard disable of all animations for the application or on
// the element itself, therefore there is no need to continue further
@@ -163,13 +163,14 @@ describe("$$AnimateRunner", function() {
}));

it("should use timeouts to trigger async operations when the document is hidden", function() {
var hidden = true;
var doc;

module(function($provide) {

$provide.value('$$isDocumentHidden', function() {
return hidden;
doc = jqLite({
body: document.body,
hidden: true
});
$provide.value('$document', doc);
});

inject(function($$AnimateRunner, $rootScope, $$rAF, $timeout) {
@@ -183,7 +184,7 @@ describe("$$AnimateRunner", function() {
$timeout.flush();
expect(spy).toHaveBeenCalled();

hidden = false;
doc[0].hidden = false;

spy = jasmine.createSpy();
runner = new $$AnimateRunner();
@@ -6087,22 +6087,22 @@ describe('$compile', function() {
});

inject(function($compile, $rootScope) {
var cacheSize = jqLiteCacheSize();
expect(jqLiteCacheSize()).toEqual(0);

element = $compile('<div><div ng-repeat="x in xs" ng-if="x==1">{{x}}</div></div>')($rootScope);
expect(jqLiteCacheSize()).toEqual(cacheSize + 1);
expect(jqLiteCacheSize()).toEqual(1);

$rootScope.$apply('xs = [0,1]');
expect(jqLiteCacheSize()).toEqual(cacheSize + 2);
expect(jqLiteCacheSize()).toEqual(2);

$rootScope.$apply('xs = [0]');
expect(jqLiteCacheSize()).toEqual(cacheSize + 1);
expect(jqLiteCacheSize()).toEqual(1);

$rootScope.$apply('xs = []');
expect(jqLiteCacheSize()).toEqual(cacheSize + 1);
expect(jqLiteCacheSize()).toEqual(1);

element.remove();
expect(jqLiteCacheSize()).toEqual(cacheSize + 0);
expect(jqLiteCacheSize()).toEqual(0);
});
});

@@ -6119,22 +6119,22 @@ describe('$compile', function() {
});

inject(function($compile, $rootScope) {
var cacheSize = jqLiteCacheSize();
expect(jqLiteCacheSize()).toEqual(0);

element = $compile('<div><div ng-repeat="x in xs" ng-if="x==1">{{x}}</div></div>')($rootScope);
expect(jqLiteCacheSize()).toEqual(cacheSize);
expect(jqLiteCacheSize()).toEqual(0);

$rootScope.$apply('xs = [0,1]');
expect(jqLiteCacheSize()).toEqual(cacheSize);
expect(jqLiteCacheSize()).toEqual(0);

$rootScope.$apply('xs = [0]');
expect(jqLiteCacheSize()).toEqual(cacheSize);
expect(jqLiteCacheSize()).toEqual(0);

$rootScope.$apply('xs = []');
expect(jqLiteCacheSize()).toEqual(cacheSize);
expect(jqLiteCacheSize()).toEqual(0);

element.remove();
expect(jqLiteCacheSize()).toEqual(cacheSize);
expect(jqLiteCacheSize()).toEqual(0);
});
});

@@ -6150,26 +6150,26 @@ describe('$compile', function() {
});

inject(function($compile, $rootScope) {
var cacheSize = jqLiteCacheSize();
expect(jqLiteCacheSize()).toEqual(0);
element = $compile('<div><div ng-repeat="x in xs" ng-if="val">{{x}}</div></div>')($rootScope);

$rootScope.$apply('xs = [0,1]');
// At this point we have a bunch of comment placeholders but no real transcluded elements
// So the cache only contains the root element's data
expect(jqLiteCacheSize()).toEqual(cacheSize + 1);
expect(jqLiteCacheSize()).toEqual(1);

$rootScope.$apply('val = true');
// Now we have two concrete transcluded elements plus some comments so two more cache items
expect(jqLiteCacheSize()).toEqual(cacheSize + 3);
expect(jqLiteCacheSize()).toEqual(3);

$rootScope.$apply('val = false');
// Once again we only have comments so no transcluded elements and the cache is back to just
// the root element
expect(jqLiteCacheSize()).toEqual(cacheSize + 1);
expect(jqLiteCacheSize()).toEqual(1);

element.remove();
// Now we've even removed the root element along with its cache
expect(jqLiteCacheSize()).toEqual(cacheSize + 0);
expect(jqLiteCacheSize()).toEqual(0);
});
});

@@ -6206,7 +6206,6 @@ describe('$compile', function() {
});

inject(function($compile, $rootScope, $httpBackend, $timeout, $templateCache) {
var cacheSize = jqLiteCacheSize();
$httpBackend.whenGET('red.html').respond('<p>red.html</p>');
var template = $compile(
'<div ng-controller="Leak">' +
@@ -6221,7 +6220,7 @@ describe('$compile', function() {
$timeout.flush();
$httpBackend.flush();
expect(linkFn).not.toHaveBeenCalled();
expect(jqLiteCacheSize()).toEqual(cacheSize + 2);
expect(jqLiteCacheSize()).toEqual(2);

$templateCache.removeAll();
var destroyedScope = $rootScope.$new();
@@ -6984,7 +6983,9 @@ describe('$compile', function() {

it('should not leak memory with nested transclusion', function() {
inject(function($compile, $rootScope) {
var size, initialSize = jqLiteCacheSize();
var size;

expect(jqLiteCacheSize()).toEqual(0);

element = jqLite('<div><ul><li ng-repeat="n in nums">{{n}} => <i ng-if="0 === n%2">Even</i><i ng-if="1 === n%2">Odd</i></li></ul></div>');
$compile(element)($rootScope.$new());
@@ -6998,7 +6999,7 @@ describe('$compile', function() {
expect(jqLiteCacheSize()).toEqual(size);

element.remove();
expect(jqLiteCacheSize()).toEqual(initialSize);
expect(jqLiteCacheSize()).toEqual(0);
});
});
});
@@ -27,31 +27,3 @@ describe('$document', function() {
});
});
});


describe('$$isDocumentHidden', function() {
it('should return false by default', inject(function($$isDocumentHidden, $document) {
expect($$isDocumentHidden()).toBeFalsy(); // undefined in browsers that don't support visibility
}));

it('should listen on the visibilitychange event', function() {
var spy = spyOn(document, 'addEventListener').andCallThrough();

inject(function($$isDocumentHidden, $document) {
expect(spy.mostRecentCall.args[0]).toBe('visibilitychange');
expect(spy.mostRecentCall.args[1]).toEqual(jasmine.any(Function));
expect($$isDocumentHidden()).toBeFalsy(); // undefined in browsers that don't support visibility
});

});

it('should remove the listener when the $rootScope is destroyed', function() {
var spy = spyOn(document, 'removeEventListener').andCallThrough();

inject(function($$isDocumentHidden, $rootScope) {
$rootScope.$destroy();
expect(spy.mostRecentCall.args[0]).toBe('visibilitychange');
expect(spy.mostRecentCall.args[1]).toEqual(jasmine.any(Function));
});
});
});
@@ -156,12 +156,14 @@ describe("animations", function() {
}));

it("should skip animations entirely if the document is hidden", function() {
var hidden = true;
var doc;

module(function($provide) {
$provide.value('$$isDocumentHidden', function() {
return hidden;
doc = jqLite({
body: document.body,
hidden: true
});
$provide.value('$document', doc);
});

inject(function($animate, $rootScope) {
@@ -170,7 +172,7 @@ describe("animations", function() {
expect(capturedAnimation).toBeFalsy();
expect(element[0].parentNode).toEqual(parent[0]);

hidden = false;
doc[0].hidden = false;

$animate.leave(element);
$rootScope.$digest();
@@ -2282,19 +2284,18 @@ describe("animations", function() {


describe('because the document is hidden', function() {
var hidden = true;

beforeEach(function() {
module(function($provide) {
$provide.value('$$isDocumentHidden', function() {
return hidden;
});
beforeEach(module(function($provide) {
var doc = jqLite({
body: document.body,
hidden: true
});
});
$provide.value('$document', doc);
}));

it('should trigger callbacks for an enter animation',
inject(function($animate, $rootScope, $rootElement, $document) {

var callbackTriggered = false;
var spy = jasmine.createSpy();
$animate.on('enter', jqLite($document[0].body), spy);

0 comments on commit b24bfae

Please sign in to comment.
You can’t perform that action at this time.