1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/test/social/browser_share.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,174 @@ 1.4 + 1.5 +let SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService; 1.6 + 1.7 +let baseURL = "https://example.com/browser/browser/base/content/test/social/"; 1.8 + 1.9 +let manifest = { // normal provider 1.10 + name: "provider 1", 1.11 + origin: "https://example.com", 1.12 + workerURL: "https://example.com/browser/browser/base/content/test/social/social_worker.js", 1.13 + iconURL: "https://example.com/browser/browser/base/content/test/general/moz.png", 1.14 + shareURL: "https://example.com/browser/browser/base/content/test/social/share.html" 1.15 +}; 1.16 + 1.17 +function test() { 1.18 + waitForExplicitFinish(); 1.19 + 1.20 + runSocialTests(tests); 1.21 +} 1.22 + 1.23 +let corpus = [ 1.24 + { 1.25 + url: baseURL+"opengraph/opengraph.html", 1.26 + options: { 1.27 + // og:title 1.28 + title: ">This is my title<", 1.29 + // og:description 1.30 + description: "A test corpus file for open graph tags we care about", 1.31 + //medium: this.getPageMedium(), 1.32 + //source: this.getSourceURL(), 1.33 + // og:url 1.34 + url: "https://www.mozilla.org/", 1.35 + //shortUrl: this.getShortURL(), 1.36 + // og:image 1.37 + previews:["https://www.mozilla.org/favicon.png"], 1.38 + // og:site_name 1.39 + siteName: ">My simple test page<" 1.40 + } 1.41 + }, 1.42 + { 1.43 + // tests that og:url doesn't override the page url if it is bad 1.44 + url: baseURL+"opengraph/og_invalid_url.html", 1.45 + options: { 1.46 + description: "A test corpus file for open graph tags passing a bad url", 1.47 + url: baseURL+"opengraph/og_invalid_url.html", 1.48 + previews: [], 1.49 + siteName: "Evil chrome delivering website" 1.50 + } 1.51 + }, 1.52 + { 1.53 + url: baseURL+"opengraph/shorturl_link.html", 1.54 + options: { 1.55 + previews: ["http://example.com/1234/56789.jpg"], 1.56 + url: "http://www.example.com/photos/56789/", 1.57 + shortUrl: "http://imshort/p/abcde" 1.58 + } 1.59 + }, 1.60 + { 1.61 + url: baseURL+"opengraph/shorturl_linkrel.html", 1.62 + options: { 1.63 + previews: ["http://example.com/1234/56789.jpg"], 1.64 + url: "http://www.example.com/photos/56789/", 1.65 + shortUrl: "http://imshort/p/abcde" 1.66 + } 1.67 + }, 1.68 + { 1.69 + url: baseURL+"opengraph/shortlink_linkrel.html", 1.70 + options: { 1.71 + previews: ["http://example.com/1234/56789.jpg"], 1.72 + url: "http://www.example.com/photos/56789/", 1.73 + shortUrl: "http://imshort/p/abcde" 1.74 + } 1.75 + } 1.76 +]; 1.77 + 1.78 +function loadURLInTab(url, callback) { 1.79 + info("Loading tab with "+url); 1.80 + let tab = gBrowser.selectedTab = gBrowser.addTab(url); 1.81 + tab.linkedBrowser.addEventListener("load", function listener() { 1.82 + is(tab.linkedBrowser.currentURI.spec, url, "tab loaded") 1.83 + tab.linkedBrowser.removeEventListener("load", listener, true); 1.84 + executeSoon(function() { callback(tab) }); 1.85 + }, true); 1.86 +} 1.87 + 1.88 +function hasoptions(testOptions, options) { 1.89 + let msg; 1.90 + for (let option in testOptions) { 1.91 + let data = testOptions[option]; 1.92 + info("data: "+JSON.stringify(data)); 1.93 + let message_data = options[option]; 1.94 + info("message_data: "+JSON.stringify(message_data)); 1.95 + if (Array.isArray(data)) { 1.96 + // the message may have more array elements than we are testing for, this 1.97 + // is ok since some of those are hard to test. So we just test that 1.98 + // anything in our test data IS in the message. 1.99 + ok(Array.every(data, function(item) { return message_data.indexOf(item) >= 0 }), "option "+option); 1.100 + } else { 1.101 + is(message_data, data, "option "+option); 1.102 + } 1.103 + } 1.104 +} 1.105 + 1.106 +var tests = { 1.107 + testShareDisabledOnActivation: function(next) { 1.108 + // starting on about:blank page, share should be visible but disabled when 1.109 + // adding provider 1.110 + is(gBrowser.contentDocument.location.href, "about:blank"); 1.111 + SocialService.addProvider(manifest, function(provider) { 1.112 + is(SocialUI.enabled, true, "SocialUI is enabled"); 1.113 + checkSocialUI(); 1.114 + // share should not be enabled since we only have about:blank page 1.115 + let shareButton = SocialShare.shareButton; 1.116 + is(shareButton.disabled, true, "share button is disabled"); 1.117 + // verify the attribute for proper css 1.118 + is(shareButton.getAttribute("disabled"), "true", "share button attribute is disabled"); 1.119 + // button should be visible 1.120 + is(shareButton.hidden, false, "share button is visible"); 1.121 + SocialService.removeProvider(manifest.origin, next); 1.122 + }); 1.123 + }, 1.124 + testShareEnabledOnActivation: function(next) { 1.125 + // starting from *some* page, share should be visible and enabled when 1.126 + // activating provider 1.127 + let testData = corpus[0]; 1.128 + loadURLInTab(testData.url, function(tab) { 1.129 + SocialService.addProvider(manifest, function(provider) { 1.130 + is(SocialUI.enabled, true, "SocialUI is enabled"); 1.131 + checkSocialUI(); 1.132 + // share should not be enabled since we only have about:blank page 1.133 + let shareButton = SocialShare.shareButton; 1.134 + is(shareButton.disabled, false, "share button is enabled"); 1.135 + // verify the attribute for proper css 1.136 + ok(!shareButton.hasAttribute("disabled"), "share button is enabled"); 1.137 + // button should be visible 1.138 + is(shareButton.hidden, false, "share button is visible"); 1.139 + gBrowser.removeTab(tab); 1.140 + next(); 1.141 + }); 1.142 + }); 1.143 + }, 1.144 + testSharePage: function(next) { 1.145 + let provider = Social._getProviderFromOrigin(manifest.origin); 1.146 + let port = provider.getWorkerPort(); 1.147 + ok(port, "provider has a port"); 1.148 + let testTab; 1.149 + let testIndex = 0; 1.150 + let testData = corpus[testIndex++]; 1.151 + 1.152 + function runOneTest() { 1.153 + loadURLInTab(testData.url, function(tab) { 1.154 + testTab = tab; 1.155 + SocialShare.sharePage(); 1.156 + }); 1.157 + } 1.158 + 1.159 + port.onmessage = function (e) { 1.160 + let topic = e.data.topic; 1.161 + switch (topic) { 1.162 + case "got-share-data-message": 1.163 + gBrowser.removeTab(testTab); 1.164 + hasoptions(testData.options, e.data.result); 1.165 + testData = corpus[testIndex++]; 1.166 + if (testData) { 1.167 + executeSoon(runOneTest); 1.168 + } else { 1.169 + SocialService.removeProvider(manifest.origin, next); 1.170 + } 1.171 + break; 1.172 + } 1.173 + } 1.174 + port.postMessage({topic: "test-init"}); 1.175 + executeSoon(runOneTest); 1.176 + } 1.177 +}