michael@0: michael@0: let SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService; michael@0: michael@0: let baseURL = "https://example.com/browser/browser/base/content/test/social/"; michael@0: michael@0: let manifest = { // normal provider michael@0: name: "provider 1", michael@0: origin: "https://example.com", michael@0: workerURL: "https://example.com/browser/browser/base/content/test/social/social_worker.js", michael@0: iconURL: "https://example.com/browser/browser/base/content/test/general/moz.png", michael@0: shareURL: "https://example.com/browser/browser/base/content/test/social/share.html" michael@0: }; michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: runSocialTests(tests); michael@0: } michael@0: michael@0: let corpus = [ michael@0: { michael@0: url: baseURL+"opengraph/opengraph.html", michael@0: options: { michael@0: // og:title michael@0: title: ">This is my title<", michael@0: // og:description michael@0: description: "A test corpus file for open graph tags we care about", michael@0: //medium: this.getPageMedium(), michael@0: //source: this.getSourceURL(), michael@0: // og:url michael@0: url: "https://www.mozilla.org/", michael@0: //shortUrl: this.getShortURL(), michael@0: // og:image michael@0: previews:["https://www.mozilla.org/favicon.png"], michael@0: // og:site_name michael@0: siteName: ">My simple test page<" michael@0: } michael@0: }, michael@0: { michael@0: // tests that og:url doesn't override the page url if it is bad michael@0: url: baseURL+"opengraph/og_invalid_url.html", michael@0: options: { michael@0: description: "A test corpus file for open graph tags passing a bad url", michael@0: url: baseURL+"opengraph/og_invalid_url.html", michael@0: previews: [], michael@0: siteName: "Evil chrome delivering website" michael@0: } michael@0: }, michael@0: { michael@0: url: baseURL+"opengraph/shorturl_link.html", michael@0: options: { michael@0: previews: ["http://example.com/1234/56789.jpg"], michael@0: url: "http://www.example.com/photos/56789/", michael@0: shortUrl: "http://imshort/p/abcde" michael@0: } michael@0: }, michael@0: { michael@0: url: baseURL+"opengraph/shorturl_linkrel.html", michael@0: options: { michael@0: previews: ["http://example.com/1234/56789.jpg"], michael@0: url: "http://www.example.com/photos/56789/", michael@0: shortUrl: "http://imshort/p/abcde" michael@0: } michael@0: }, michael@0: { michael@0: url: baseURL+"opengraph/shortlink_linkrel.html", michael@0: options: { michael@0: previews: ["http://example.com/1234/56789.jpg"], michael@0: url: "http://www.example.com/photos/56789/", michael@0: shortUrl: "http://imshort/p/abcde" michael@0: } michael@0: } michael@0: ]; michael@0: michael@0: function loadURLInTab(url, callback) { michael@0: info("Loading tab with "+url); michael@0: let tab = gBrowser.selectedTab = gBrowser.addTab(url); michael@0: tab.linkedBrowser.addEventListener("load", function listener() { michael@0: is(tab.linkedBrowser.currentURI.spec, url, "tab loaded") michael@0: tab.linkedBrowser.removeEventListener("load", listener, true); michael@0: executeSoon(function() { callback(tab) }); michael@0: }, true); michael@0: } michael@0: michael@0: function hasoptions(testOptions, options) { michael@0: let msg; michael@0: for (let option in testOptions) { michael@0: let data = testOptions[option]; michael@0: info("data: "+JSON.stringify(data)); michael@0: let message_data = options[option]; michael@0: info("message_data: "+JSON.stringify(message_data)); michael@0: if (Array.isArray(data)) { michael@0: // the message may have more array elements than we are testing for, this michael@0: // is ok since some of those are hard to test. So we just test that michael@0: // anything in our test data IS in the message. michael@0: ok(Array.every(data, function(item) { return message_data.indexOf(item) >= 0 }), "option "+option); michael@0: } else { michael@0: is(message_data, data, "option "+option); michael@0: } michael@0: } michael@0: } michael@0: michael@0: var tests = { michael@0: testShareDisabledOnActivation: function(next) { michael@0: // starting on about:blank page, share should be visible but disabled when michael@0: // adding provider michael@0: is(gBrowser.contentDocument.location.href, "about:blank"); michael@0: SocialService.addProvider(manifest, function(provider) { michael@0: is(SocialUI.enabled, true, "SocialUI is enabled"); michael@0: checkSocialUI(); michael@0: // share should not be enabled since we only have about:blank page michael@0: let shareButton = SocialShare.shareButton; michael@0: is(shareButton.disabled, true, "share button is disabled"); michael@0: // verify the attribute for proper css michael@0: is(shareButton.getAttribute("disabled"), "true", "share button attribute is disabled"); michael@0: // button should be visible michael@0: is(shareButton.hidden, false, "share button is visible"); michael@0: SocialService.removeProvider(manifest.origin, next); michael@0: }); michael@0: }, michael@0: testShareEnabledOnActivation: function(next) { michael@0: // starting from *some* page, share should be visible and enabled when michael@0: // activating provider michael@0: let testData = corpus[0]; michael@0: loadURLInTab(testData.url, function(tab) { michael@0: SocialService.addProvider(manifest, function(provider) { michael@0: is(SocialUI.enabled, true, "SocialUI is enabled"); michael@0: checkSocialUI(); michael@0: // share should not be enabled since we only have about:blank page michael@0: let shareButton = SocialShare.shareButton; michael@0: is(shareButton.disabled, false, "share button is enabled"); michael@0: // verify the attribute for proper css michael@0: ok(!shareButton.hasAttribute("disabled"), "share button is enabled"); michael@0: // button should be visible michael@0: is(shareButton.hidden, false, "share button is visible"); michael@0: gBrowser.removeTab(tab); michael@0: next(); michael@0: }); michael@0: }); michael@0: }, michael@0: testSharePage: function(next) { michael@0: let provider = Social._getProviderFromOrigin(manifest.origin); michael@0: let port = provider.getWorkerPort(); michael@0: ok(port, "provider has a port"); michael@0: let testTab; michael@0: let testIndex = 0; michael@0: let testData = corpus[testIndex++]; michael@0: michael@0: function runOneTest() { michael@0: loadURLInTab(testData.url, function(tab) { michael@0: testTab = tab; michael@0: SocialShare.sharePage(); michael@0: }); michael@0: } michael@0: michael@0: port.onmessage = function (e) { michael@0: let topic = e.data.topic; michael@0: switch (topic) { michael@0: case "got-share-data-message": michael@0: gBrowser.removeTab(testTab); michael@0: hasoptions(testData.options, e.data.result); michael@0: testData = corpus[testIndex++]; michael@0: if (testData) { michael@0: executeSoon(runOneTest); michael@0: } else { michael@0: SocialService.removeProvider(manifest.origin, next); michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: port.postMessage({topic: "test-init"}); michael@0: executeSoon(runOneTest); michael@0: } michael@0: }