2012-07-11 18:31:19 -07:00
/ * T h i s S o u r c e C o d e F o r m i s s u b j e c t t o t h e t e r m s o f t h e M o z i l l a P u b l i c
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at http : //mozilla.org/MPL/2.0/. */
let prefName = "social.enabled" ,
2012-08-08 18:09:37 -07:00
gFinishCB ;
2012-07-11 18:31:19 -07:00
function test ( ) {
waitForExplicitFinish ( ) ;
2012-10-24 22:44:53 -07:00
// Need to load a http/https/ftp/ftps page for the social share button to appear
let tab = gBrowser . selectedTab = gBrowser . addTab ( "https://example.com" , { skipAnimation : true } ) ;
2012-07-11 18:31:19 -07:00
tab . linkedBrowser . addEventListener ( "load" , function tabLoad ( event ) {
tab . linkedBrowser . removeEventListener ( "load" , tabLoad , true ) ;
executeSoon ( tabLoaded ) ;
} , true ) ;
registerCleanupFunction ( function ( ) {
2012-08-11 20:55:58 -07:00
Services . prefs . clearUserPref ( prefName ) ;
2012-07-11 18:31:19 -07:00
gBrowser . removeTab ( tab ) ;
} ) ;
}
function tabLoaded ( ) {
ok ( Social , "Social module loaded" ) ;
2012-08-08 18:09:37 -07:00
let manifest = { // normal provider
name : "provider 1" ,
origin : "https://example.com" ,
sidebarURL : "https://example.com/browser/browser/base/content/test/social_sidebar.html" ,
workerURL : "https://example.com/browser/browser/base/content/test/social_worker.js" ,
2012-10-09 17:14:25 -07:00
iconURL : "https://example.com/browser/browser/base/content/test/moz.png"
2012-08-08 18:09:37 -07:00
} ;
runSocialTestWithProvider ( manifest , function ( finishcb ) {
gFinishCB = finishcb ;
testInitial ( ) ;
} ) ;
2012-07-11 18:31:19 -07:00
}
2012-08-08 18:09:37 -07:00
function testInitial ( finishcb ) {
2012-07-11 18:31:19 -07:00
ok ( Social . provider , "Social provider is active" ) ;
ok ( Social . provider . enabled , "Social provider is enabled" ) ;
2012-09-11 19:48:38 -07:00
let port = Social . provider . getWorkerPort ( ) ;
ok ( port , "Social provider has a port to its FrameWorker" ) ;
port . close ( ) ;
2012-07-11 18:31:19 -07:00
2012-09-24 20:54:34 -07:00
let { shareButton , unsharePopup } = SocialShareButton ;
2012-07-11 18:31:19 -07:00
ok ( shareButton , "share button exists" ) ;
2012-09-24 20:54:34 -07:00
ok ( unsharePopup , "share popup exists" ) ;
2012-07-11 18:31:19 -07:00
2012-09-24 20:54:34 -07:00
let okButton = document . getElementById ( "unsharePopupContinueSharingButton" ) ;
let undoButton = document . getElementById ( "unsharePopupStopSharingButton" ) ;
2012-08-27 04:16:38 -07:00
let shareStatusLabel = document . getElementById ( "share-button-status" ) ;
2012-08-24 20:11:55 -07:00
// ensure the worker initialization and handshakes are all done and we
2012-08-27 04:16:38 -07:00
// have a profile and the worker has responsed to the recommend-prompt msg.
waitForCondition ( function ( ) Social . provider . profile && SocialShareButton . promptImages != null , function ( ) {
is ( shareButton . hasAttribute ( "shared" ) , false , "Share button should not have 'shared' attribute before share button is clicked" ) ;
2012-08-24 20:11:55 -07:00
// check dom values
let profile = Social . provider . profile ;
let portrait = document . getElementById ( "socialUserPortrait" ) . getAttribute ( "src" ) ;
is ( profile . portrait , portrait , "portrait is set" ) ;
let displayName = document . getElementById ( "socialUserDisplayName" ) ;
is ( displayName . label , profile . displayName , "display name is set" ) ;
2012-09-24 20:54:34 -07:00
ok ( ! document . getElementById ( "unsharePopupHeader" ) . hidden , "user profile is visible" ) ;
2012-08-27 04:16:38 -07:00
// Check the strings from our worker actually ended up on the button.
is ( shareButton . getAttribute ( "tooltiptext" ) , "Share this page" , "check tooltip text is correct" ) ;
is ( shareStatusLabel . getAttribute ( "value" ) , "" , "check status label text is blank" ) ;
// Check the relative URL was resolved correctly (note this image has offsets of zero...)
2012-10-23 23:45:51 -07:00
is ( shareButton . style . backgroundImage , 'url("https://example.com/browser/browser/base/content/test/social_share_image.png")' , "check image url is correct" ) ;
2012-08-27 04:16:38 -07:00
2012-08-24 20:11:55 -07:00
// Test clicking the share button
shareButton . addEventListener ( "click" , function listener ( ) {
shareButton . removeEventListener ( "click" , listener ) ;
is ( shareButton . hasAttribute ( "shared" ) , true , "Share button should have 'shared' attribute after share button is clicked" ) ;
2012-08-27 04:16:38 -07:00
is ( shareButton . getAttribute ( "tooltiptext" ) , "Unshare this page" , "check tooltip text is correct" ) ;
is ( shareStatusLabel . getAttribute ( "value" ) , "This page has been shared" , "check status label text is correct" ) ;
// Check the URL and offsets were applied correctly
2012-10-23 23:45:51 -07:00
is ( shareButton . style . backgroundImage , 'url("https://example.com/browser/browser/base/content/test/social_share_image.png")' , "check image url is correct" ) ;
2012-08-24 20:11:55 -07:00
executeSoon ( testSecondClick . bind ( window , testPopupOKButton ) ) ;
} ) ;
EventUtils . synthesizeMouseAtCenter ( shareButton , { } ) ;
2012-08-27 04:16:38 -07:00
} , "provider didn't provide user-recommend-prompt response" ) ;
2012-07-11 18:31:19 -07:00
}
function testSecondClick ( nextTest ) {
2012-09-24 20:54:34 -07:00
let { shareButton , unsharePopup } = SocialShareButton ;
unsharePopup . addEventListener ( "popupshown" , function listener ( ) {
unsharePopup . removeEventListener ( "popupshown" , listener ) ;
2012-07-11 18:31:19 -07:00
ok ( true , "popup was shown after second click" ) ;
executeSoon ( nextTest ) ;
} ) ;
EventUtils . synthesizeMouseAtCenter ( shareButton , { } ) ;
}
function testPopupOKButton ( ) {
2012-09-24 20:54:34 -07:00
let { shareButton , unsharePopup } = SocialShareButton ;
let okButton = document . getElementById ( "unsharePopupContinueSharingButton" ) ;
unsharePopup . addEventListener ( "popuphidden" , function listener ( ) {
unsharePopup . removeEventListener ( "popuphidden" , listener ) ;
2012-07-11 18:31:19 -07:00
is ( shareButton . hasAttribute ( "shared" ) , true , "Share button should still have 'shared' attribute after OK button is clicked" ) ;
executeSoon ( testSecondClick . bind ( window , testPopupUndoButton ) ) ;
} ) ;
EventUtils . synthesizeMouseAtCenter ( okButton , { } ) ;
}
function testPopupUndoButton ( ) {
2012-09-24 20:54:34 -07:00
let { shareButton , unsharePopup } = SocialShareButton ;
let undoButton = document . getElementById ( "unsharePopupStopSharingButton" ) ;
unsharePopup . addEventListener ( "popuphidden" , function listener ( ) {
unsharePopup . removeEventListener ( "popuphidden" , listener ) ;
2012-07-11 18:31:19 -07:00
is ( shareButton . hasAttribute ( "shared" ) , false , "Share button should not have 'shared' attribute after Undo button is clicked" ) ;
executeSoon ( testShortcut ) ;
} ) ;
EventUtils . synthesizeMouseAtCenter ( undoButton , { } ) ;
}
function testShortcut ( ) {
let keyTarget = window ;
keyTarget . addEventListener ( "keyup" , function listener ( ) {
keyTarget . removeEventListener ( "keyup" , listener ) ;
executeSoon ( checkShortcutWorked . bind ( window , keyTarget ) ) ;
} ) ;
EventUtils . synthesizeKey ( "l" , { accelKey : true , shiftKey : true } , keyTarget ) ;
}
function checkShortcutWorked ( keyTarget ) {
2012-09-24 20:54:34 -07:00
let { unsharePopup , shareButton } = SocialShareButton ;
2012-07-11 18:31:19 -07:00
is ( shareButton . hasAttribute ( "shared" ) , true , "Share button should be in the 'shared' state after keyboard shortcut is used" ) ;
// Test a second invocation of the shortcut
2012-09-24 20:54:34 -07:00
unsharePopup . addEventListener ( "popupshown" , function listener ( ) {
unsharePopup . removeEventListener ( "popupshown" , listener ) ;
2012-07-11 18:31:19 -07:00
ok ( true , "popup was shown after second use of keyboard shortcut" ) ;
executeSoon ( checkOKButton ) ;
} ) ;
EventUtils . synthesizeKey ( "l" , { accelKey : true , shiftKey : true } , keyTarget ) ;
}
function checkOKButton ( ) {
2012-09-24 20:54:34 -07:00
let okButton = document . getElementById ( "unsharePopupContinueSharingButton" ) ;
let undoButton = document . getElementById ( "unsharePopupStopSharingButton" ) ;
2012-07-11 18:31:19 -07:00
is ( document . activeElement , okButton , "ok button should be focused by default" ) ;
2012-09-24 20:54:34 -07:00
// the undo button text, label text, access keys, etc should be as
// specified by the provider.
function isEltAttr ( eltid , attr , expected ) {
is ( document . getElementById ( eltid ) . getAttribute ( attr ) , expected ,
"element '" + eltid + "' has correct value for attribute '" + attr + "'" ) ;
}
isEltAttr ( "socialUserRecommendedText" , "value" , "You have already shared this page" ) ;
isEltAttr ( "unsharePopupContinueSharingButton" , "label" , "Got it!" ) ;
isEltAttr ( "unsharePopupContinueSharingButton" , "accesskey" , "G" ) ;
isEltAttr ( "unsharePopupStopSharingButton" , "label" , "Unshare it!" ) ;
isEltAttr ( "unsharePopupStopSharingButton" , "accesskey" , "U" ) ;
isEltAttr ( "socialUserPortrait" , "aria-label" , "Your pretty face" ) ;
2012-08-08 18:09:37 -07:00
// This rest of particular test doesn't really apply on Mac, since buttons
// aren't focusable by default.
2012-09-08 13:58:23 -07:00
if ( navigator . platform . contains ( "Mac" ) ) {
2012-08-08 18:09:37 -07:00
executeSoon ( testCloseBySpace ) ;
2012-07-11 18:31:19 -07:00
return ;
}
2012-08-08 18:09:37 -07:00
let displayName = document . getElementById ( "socialUserDisplayName" ) ;
// Linux has the buttons in the [unshare] [ok] order, so displayName will come first.
2012-09-08 13:58:23 -07:00
if ( navigator . platform . contains ( "Linux" ) ) {
2012-08-08 18:09:37 -07:00
checkNextInTabOrder ( displayName , function ( ) {
checkNextInTabOrder ( undoButton , function ( ) {
checkNextInTabOrder ( okButton , testCloseBySpace ) ;
} ) ;
} ) ;
} else {
checkNextInTabOrder ( undoButton , function ( ) {
checkNextInTabOrder ( displayName , function ( ) {
checkNextInTabOrder ( okButton , testCloseBySpace ) ;
} ) ;
} ) ;
}
}
function checkNextInTabOrder ( element , next ) {
2012-07-11 18:31:19 -07:00
function listener ( ) {
element . removeEventListener ( "focus" , listener ) ;
is ( document . activeElement , element , element . id + " should be next in tab order" ) ;
executeSoon ( next ) ;
}
element . addEventListener ( "focus" , listener ) ;
// Register a cleanup function to remove the listener in case this test fails
registerCleanupFunction ( function ( ) {
element . removeEventListener ( "focus" , listener ) ;
} ) ;
EventUtils . synthesizeKey ( "VK_TAB" , { } ) ;
}
function testCloseBySpace ( ) {
2012-09-24 20:54:34 -07:00
let unsharePopup = SocialShareButton . unsharePopup ;
is ( document . activeElement . id , "unsharePopupContinueSharingButton" , "testCloseBySpace, the ok button should be focused" ) ;
unsharePopup . addEventListener ( "popuphidden" , function listener ( ) {
unsharePopup . removeEventListener ( "popuphidden" , listener ) ;
2012-07-11 18:31:19 -07:00
ok ( true , "space closed the share popup" ) ;
2012-09-12 19:05:06 -07:00
executeSoon ( testStillSharedIn2Tabs ) ;
2012-07-11 18:31:19 -07:00
} ) ;
EventUtils . synthesizeKey ( "VK_SPACE" , { } ) ;
}
2012-09-12 19:05:06 -07:00
function testStillSharedIn2Tabs ( ) {
let toShare = "http://example.com" ;
let { shareButton } = SocialShareButton ;
let initialTab = gBrowser . selectedTab ;
if ( shareButton . hasAttribute ( "shared" ) ) {
SocialShareButton . unsharePage ( ) ;
}
is ( shareButton . hasAttribute ( "shared" ) , false , "Share button should not have 'shared' for the initial tab" ) ;
let tab1 = gBrowser . selectedTab = gBrowser . addTab ( toShare ) ;
let tab1b = gBrowser . getBrowserForTab ( tab1 ) ;
tab1b . addEventListener ( "load" , function tabLoad ( event ) {
tab1b . removeEventListener ( "load" , tabLoad , true ) ;
let tab2 = gBrowser . selectedTab = gBrowser . addTab ( toShare ) ;
let tab2b = gBrowser . getBrowserForTab ( tab2 ) ;
tab2b . addEventListener ( "load" , function tabLoad ( event ) {
tab2b . removeEventListener ( "load" , tabLoad , true ) ;
// should start without either page being shared.
is ( shareButton . hasAttribute ( "shared" ) , false , "Share button should not have 'shared' before we've done anything" ) ;
EventUtils . synthesizeMouseAtCenter ( shareButton , { } ) ;
is ( shareButton . hasAttribute ( "shared" ) , true , "Share button should reflect the share" ) ;
// and switching to the first tab (with the same URL) should still reflect shared.
gBrowser . selectedTab = tab1 ;
is ( shareButton . hasAttribute ( "shared" ) , true , "Share button should reflect the share" ) ;
// but switching back the initial one should reflect not shared.
gBrowser . selectedTab = initialTab ;
is ( shareButton . hasAttribute ( "shared" ) , false , "Initial tab should not reflect shared" ) ;
gBrowser . selectedTab = tab1 ;
SocialShareButton . unsharePage ( ) ;
gBrowser . removeTab ( tab1 ) ;
gBrowser . removeTab ( tab2 ) ;
executeSoon ( testStillSharedAfterReopen ) ;
} , true ) ;
} , true ) ;
}
function testStillSharedAfterReopen ( ) {
let toShare = "http://example.com" ;
let { shareButton } = SocialShareButton ;
is ( shareButton . hasAttribute ( "shared" ) , false , "Reopen: Share button should not have 'shared' for the initial tab" ) ;
let tab = gBrowser . selectedTab = gBrowser . addTab ( toShare ) ;
let tabb = gBrowser . getBrowserForTab ( tab ) ;
tabb . addEventListener ( "load" , function tabLoad ( event ) {
tabb . removeEventListener ( "load" , tabLoad , true ) ;
SocialShareButton . sharePage ( ) ;
is ( shareButton . hasAttribute ( "shared" ) , true , "Share button should reflect the share" ) ;
gBrowser . removeTab ( tab ) ;
// should be on the initial unshared tab now.
is ( shareButton . hasAttribute ( "shared" ) , false , "Initial tab should be selected and be unshared." ) ;
// now open the same URL - should be back to shared.
tab = gBrowser . selectedTab = gBrowser . addTab ( toShare , { skipAnimation : true } ) ;
tab . linkedBrowser . addEventListener ( "load" , function tabLoad ( event ) {
tab . linkedBrowser . removeEventListener ( "load" , tabLoad , true ) ;
2012-10-24 22:44:53 -07:00
executeSoon ( function ( ) {
is ( shareButton . hasAttribute ( "shared" ) , true , "New tab to previously shared URL should reflect shared" ) ;
SocialShareButton . unsharePage ( ) ;
gBrowser . removeTab ( tab ) ;
executeSoon ( testOnlyShareCertainUrlsTabSwitch ) ;
} ) ;
} , true ) ;
} , true ) ;
}
function testOnlyShareCertainUrlsTabSwitch ( ) {
let toShare = "http://example.com" ;
let notSharable = "about:blank" ;
let { shareButton } = SocialShareButton ;
let tab = gBrowser . selectedTab = gBrowser . addTab ( toShare ) ;
let tabb = gBrowser . getBrowserForTab ( tab ) ;
tabb . addEventListener ( "load" , function tabLoad ( event ) {
tabb . removeEventListener ( "load" , tabLoad , true ) ;
ok ( ! shareButton . hidden , "share button not hidden for http url" ) ;
let tab2 = gBrowser . selectedTab = gBrowser . addTab ( notSharable ) ;
let tabb2 = gBrowser . getBrowserForTab ( tab2 ) ;
tabb2 . addEventListener ( "load" , function tabLoad ( event ) {
tabb2 . removeEventListener ( "load" , tabLoad , true ) ;
ok ( shareButton . hidden , "share button hidden for about:blank" ) ;
gBrowser . selectedTab = tab ;
ok ( ! shareButton . hidden , "share button re-shown when switching back to http: url" ) ;
gBrowser . selectedTab = tab2 ;
ok ( shareButton . hidden , "share button re-hidden when switching back to about:blank" ) ;
2012-09-12 19:05:06 -07:00
gBrowser . removeTab ( tab ) ;
2012-10-24 22:44:53 -07:00
gBrowser . removeTab ( tab2 ) ;
executeSoon ( testOnlyShareCertainUrlsSameTab ) ;
} , true ) ;
} , true ) ;
}
function testOnlyShareCertainUrlsSameTab ( ) {
let toShare = "http://example.com" ;
let notSharable = "about:blank" ;
let { shareButton } = SocialShareButton ;
let tab = gBrowser . selectedTab = gBrowser . addTab ( toShare ) ;
let tabb = gBrowser . getBrowserForTab ( tab ) ;
tabb . addEventListener ( "load" , function tabLoad ( event ) {
tabb . removeEventListener ( "load" , tabLoad , true ) ;
ok ( ! shareButton . hidden , "share button not hidden for http url" ) ;
tabb . addEventListener ( "load" , function tabLoad ( event ) {
tabb . removeEventListener ( "load" , tabLoad , true ) ;
ok ( shareButton . hidden , "share button hidden for about:blank" ) ;
tabb . addEventListener ( "load" , function tabLoad ( event ) {
tabb . removeEventListener ( "load" , tabLoad , true ) ;
ok ( ! shareButton . hidden , "share button re-enabled http url" ) ;
gBrowser . removeTab ( tab ) ;
executeSoon ( testDisable ) ;
} , true ) ;
tabb . loadURI ( toShare ) ;
2012-09-12 19:05:06 -07:00
} , true ) ;
2012-10-24 22:44:53 -07:00
tabb . loadURI ( notSharable ) ;
2012-09-12 19:05:06 -07:00
} , true ) ;
}
2012-07-11 18:31:19 -07:00
function testDisable ( ) {
2012-08-24 20:11:55 -07:00
let shareButton = SocialShareButton . shareButton ;
2012-07-11 18:31:19 -07:00
Services . prefs . setBoolPref ( prefName , false ) ;
is ( shareButton . hidden , true , "Share button should be hidden when pref is disabled" ) ;
2012-08-08 18:09:37 -07:00
gFinishCB ( ) ;
2012-07-11 18:31:19 -07:00
}