Skip to content

Commit

Permalink
fix(rotate): Rotate event is not fired In certain cases (naver#188)
Browse files Browse the repository at this point in the history
* fix(rotate): Rotate event is not fired In certain cases

Fix naver#182
Close naver#188
  • Loading branch information
jongmoon committed May 2, 2016
1 parent 28eb48f commit 5a657cc
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/customEvent/rotate.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ eg.module("rotate", ["jQuery", eg, window, document], function($, ns, global, do
var agent = ns.agent();
var isMobile = /android|ios/.test(agent.os.name);

if (!isMobile) {
ns.isPortrait = function() {
return;
};

return;
}

/**
* Return event name string for orientationChange according browser support
*/
Expand Down Expand Up @@ -89,8 +97,6 @@ eg.module("rotate", ["jQuery", eg, window, document], function($, ns, global, do
vertical = false;
}
}

beforeScreenWidth = screenWidth;
} else {
degree = global.orientation;
if (degree === 0 || degree === 180) {
Expand All @@ -110,6 +116,7 @@ eg.module("rotate", ["jQuery", eg, window, document], function($, ns, global, do
if (isMobile) {
if (beforeVertical !== currentVertical) {
beforeVertical = currentVertical;
beforeScreenWidth = doc.documentElement.clientWidth;
$(global).trigger("rotate");
}
}
Expand Down Expand Up @@ -140,7 +147,6 @@ eg.module("rotate", ["jQuery", eg, window, document], function($, ns, global, do
// When width value wasn't changed after firing orientationchange, then call handler again after 300ms.
return false;
}
beforeScreenWidth = screenWidth;
}

global.clearTimeout(rotateTimer);
Expand Down
6 changes: 6 additions & 0 deletions test/manual/rotate.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
<div id="layer"></div>
<script>
$(document).ready(function(){
$(window).on("resize", function() {
if (eg.isPortrait()) {
//do something...Test if call of isPortrait() makes side effects.
}
});

$(window).on("rotate",function(e){
$("#layer").text(e.isVertical?"vertical":"horizontal");
});
Expand Down
96 changes: 95 additions & 1 deletion test/unit/js/rotate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ test("isVertical : If event is resize then sencond times call. and stay.", funct
var isVertical = method.isVertical();

// Then
equal(isVertical,null);
equal(isVertical,true);
});

test("isVertical : If event is resize then sencond times call. and rotate horizontal.", function() {
Expand Down Expand Up @@ -442,3 +442,97 @@ test("test using isPortrait first", function() {
// Then
equal(typeof val,"boolean");
});

test("If eg.isPortrait() affect the rotate event not to be fired.", function() {
// Given
var isCall = false;
var isVertical1 = false;
var isVertical2 = false;
var agent = eg.agent();
agent.os = {
name: "android",
version: "2.1"
};

delete this.fakeWindow.onorientationchange;
this.fakeWindow.resize = "resize";

this.fakeDocument.documentElement.clientWidth = 200;
this.fakeDocument.documentElement.clientHeight = 100;

var method = eg.invoke("rotate",[jQuery, null, this.fakeWindow, this.fakeDocument]);
$(this.fakeWindow).on("rotate",function(e){
isCall = true;
isVertical2 = e.isVertical;
});

// When
this.fakeDocument.documentElement.clientWidth = 100;
this.fakeDocument.documentElement.clientHeight = 200;

// isVertical() should not affect isVertical2.
isVertical1 = method.isVertical();

method.handler({
type : "resize"
});
this.clock.tick( 10 );

// Then
ok(isCall, "rotate event is fired by resize");
ok(isVertical1, "Does isVertical return true?");
ok(isVertical2, "Does rotate event handler get vertical?");

// When
isCall = false;
agent.os = {
name: "android",
version: "4.3"
};
this.fakeWindow.onorientationchange = noop;
this.fakeWindow.orientation = 90;

method = eg.invoke("rotate",[jQuery, null, this.fakeWindow, this.fakeDocument]);
$(this.fakeWindow).on("rotate",function(e){
isCall = true;
isVertical2 = e.isVertical;
});

// isVertical() should not affect isVertical2.
isVertical1 = method.isVertical();
method.handler({
type : "orientationchange"
});

this.clock.tick( 500 );

// Then
ok(isCall, "rotate event is fired by onorientationchange");
ok(!isVertical1, "Does isVertical return false?");
ok(!isVertical2, "Does rotate event handler get horizontal?");
});

test("orientationChange : mac(PC) ", function() {
// Given
var agent = eg.agent();
agent.os = {
name: "mac",
};

var method = eg.invoke("rotate",[jQuery, null, this.fakeWindow, this.fakeDocument]);

// When
// Then
equal(method, null, "Invocation of rotate in mac Browser returns null");

//Given
agent.os = {
name: "windows",
};

method = eg.invoke("rotate",[jQuery, null, this.fakeWindow, this.fakeDocument]);

// When
// Then
equal(method, null, "Invocation of rotate in windows Browser returns null");
});

0 comments on commit 5a657cc

Please sign in to comment.