|
1 |
|
2 let SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService; |
|
3 |
|
4 let baseURL = "https://example.com/browser/browser/base/content/test/social/"; |
|
5 |
|
6 let manifest = { // normal provider |
|
7 name: "provider 1", |
|
8 origin: "https://example.com", |
|
9 workerURL: "https://example.com/browser/browser/base/content/test/social/social_worker.js", |
|
10 iconURL: "https://example.com/browser/browser/base/content/test/general/moz.png", |
|
11 shareURL: "https://example.com/browser/browser/base/content/test/social/share.html" |
|
12 }; |
|
13 |
|
14 function test() { |
|
15 waitForExplicitFinish(); |
|
16 |
|
17 runSocialTests(tests); |
|
18 } |
|
19 |
|
20 let corpus = [ |
|
21 { |
|
22 url: baseURL+"opengraph/opengraph.html", |
|
23 options: { |
|
24 // og:title |
|
25 title: ">This is my title<", |
|
26 // og:description |
|
27 description: "A test corpus file for open graph tags we care about", |
|
28 //medium: this.getPageMedium(), |
|
29 //source: this.getSourceURL(), |
|
30 // og:url |
|
31 url: "https://www.mozilla.org/", |
|
32 //shortUrl: this.getShortURL(), |
|
33 // og:image |
|
34 previews:["https://www.mozilla.org/favicon.png"], |
|
35 // og:site_name |
|
36 siteName: ">My simple test page<" |
|
37 } |
|
38 }, |
|
39 { |
|
40 // tests that og:url doesn't override the page url if it is bad |
|
41 url: baseURL+"opengraph/og_invalid_url.html", |
|
42 options: { |
|
43 description: "A test corpus file for open graph tags passing a bad url", |
|
44 url: baseURL+"opengraph/og_invalid_url.html", |
|
45 previews: [], |
|
46 siteName: "Evil chrome delivering website" |
|
47 } |
|
48 }, |
|
49 { |
|
50 url: baseURL+"opengraph/shorturl_link.html", |
|
51 options: { |
|
52 previews: ["http://example.com/1234/56789.jpg"], |
|
53 url: "http://www.example.com/photos/56789/", |
|
54 shortUrl: "http://imshort/p/abcde" |
|
55 } |
|
56 }, |
|
57 { |
|
58 url: baseURL+"opengraph/shorturl_linkrel.html", |
|
59 options: { |
|
60 previews: ["http://example.com/1234/56789.jpg"], |
|
61 url: "http://www.example.com/photos/56789/", |
|
62 shortUrl: "http://imshort/p/abcde" |
|
63 } |
|
64 }, |
|
65 { |
|
66 url: baseURL+"opengraph/shortlink_linkrel.html", |
|
67 options: { |
|
68 previews: ["http://example.com/1234/56789.jpg"], |
|
69 url: "http://www.example.com/photos/56789/", |
|
70 shortUrl: "http://imshort/p/abcde" |
|
71 } |
|
72 } |
|
73 ]; |
|
74 |
|
75 function loadURLInTab(url, callback) { |
|
76 info("Loading tab with "+url); |
|
77 let tab = gBrowser.selectedTab = gBrowser.addTab(url); |
|
78 tab.linkedBrowser.addEventListener("load", function listener() { |
|
79 is(tab.linkedBrowser.currentURI.spec, url, "tab loaded") |
|
80 tab.linkedBrowser.removeEventListener("load", listener, true); |
|
81 executeSoon(function() { callback(tab) }); |
|
82 }, true); |
|
83 } |
|
84 |
|
85 function hasoptions(testOptions, options) { |
|
86 let msg; |
|
87 for (let option in testOptions) { |
|
88 let data = testOptions[option]; |
|
89 info("data: "+JSON.stringify(data)); |
|
90 let message_data = options[option]; |
|
91 info("message_data: "+JSON.stringify(message_data)); |
|
92 if (Array.isArray(data)) { |
|
93 // the message may have more array elements than we are testing for, this |
|
94 // is ok since some of those are hard to test. So we just test that |
|
95 // anything in our test data IS in the message. |
|
96 ok(Array.every(data, function(item) { return message_data.indexOf(item) >= 0 }), "option "+option); |
|
97 } else { |
|
98 is(message_data, data, "option "+option); |
|
99 } |
|
100 } |
|
101 } |
|
102 |
|
103 var tests = { |
|
104 testShareDisabledOnActivation: function(next) { |
|
105 // starting on about:blank page, share should be visible but disabled when |
|
106 // adding provider |
|
107 is(gBrowser.contentDocument.location.href, "about:blank"); |
|
108 SocialService.addProvider(manifest, function(provider) { |
|
109 is(SocialUI.enabled, true, "SocialUI is enabled"); |
|
110 checkSocialUI(); |
|
111 // share should not be enabled since we only have about:blank page |
|
112 let shareButton = SocialShare.shareButton; |
|
113 is(shareButton.disabled, true, "share button is disabled"); |
|
114 // verify the attribute for proper css |
|
115 is(shareButton.getAttribute("disabled"), "true", "share button attribute is disabled"); |
|
116 // button should be visible |
|
117 is(shareButton.hidden, false, "share button is visible"); |
|
118 SocialService.removeProvider(manifest.origin, next); |
|
119 }); |
|
120 }, |
|
121 testShareEnabledOnActivation: function(next) { |
|
122 // starting from *some* page, share should be visible and enabled when |
|
123 // activating provider |
|
124 let testData = corpus[0]; |
|
125 loadURLInTab(testData.url, function(tab) { |
|
126 SocialService.addProvider(manifest, function(provider) { |
|
127 is(SocialUI.enabled, true, "SocialUI is enabled"); |
|
128 checkSocialUI(); |
|
129 // share should not be enabled since we only have about:blank page |
|
130 let shareButton = SocialShare.shareButton; |
|
131 is(shareButton.disabled, false, "share button is enabled"); |
|
132 // verify the attribute for proper css |
|
133 ok(!shareButton.hasAttribute("disabled"), "share button is enabled"); |
|
134 // button should be visible |
|
135 is(shareButton.hidden, false, "share button is visible"); |
|
136 gBrowser.removeTab(tab); |
|
137 next(); |
|
138 }); |
|
139 }); |
|
140 }, |
|
141 testSharePage: function(next) { |
|
142 let provider = Social._getProviderFromOrigin(manifest.origin); |
|
143 let port = provider.getWorkerPort(); |
|
144 ok(port, "provider has a port"); |
|
145 let testTab; |
|
146 let testIndex = 0; |
|
147 let testData = corpus[testIndex++]; |
|
148 |
|
149 function runOneTest() { |
|
150 loadURLInTab(testData.url, function(tab) { |
|
151 testTab = tab; |
|
152 SocialShare.sharePage(); |
|
153 }); |
|
154 } |
|
155 |
|
156 port.onmessage = function (e) { |
|
157 let topic = e.data.topic; |
|
158 switch (topic) { |
|
159 case "got-share-data-message": |
|
160 gBrowser.removeTab(testTab); |
|
161 hasoptions(testData.options, e.data.result); |
|
162 testData = corpus[testIndex++]; |
|
163 if (testData) { |
|
164 executeSoon(runOneTest); |
|
165 } else { |
|
166 SocialService.removeProvider(manifest.origin, next); |
|
167 } |
|
168 break; |
|
169 } |
|
170 } |
|
171 port.postMessage({topic: "test-init"}); |
|
172 executeSoon(runOneTest); |
|
173 } |
|
174 } |