Bug 1139496 - Allow server provided explanation / ad group name to be displayed on Suggested Tiles [r=adw, f=bsmedberg]

This commit is contained in:
Marina Samuel 2015-05-19 12:42:08 -07:00
parent 6a7774865a
commit 73e89e4286
5 changed files with 131 additions and 22 deletions

View File

@ -116,6 +116,26 @@ Site.prototype = {
}
},
_newTabString: function(str, substrArr) {
let regExp = /%[0-9]\$S/g;
let matches;
while (matches = regExp.exec(str)) {
let match = matches[0];
let index = match.charAt(1); // Get the digit in the regExp.
str = str.replace(match, substrArr[index - 1]);
}
return str;
},
_getSuggestedTileExplanation: function() {
let targetedName = `<strong> ${this.link.targetedName} </strong>`;
let targetedSite = `<strong> ${this.link.targetedSite} </strong>`;
if (this.link.explanation) {
return this._newTabString(this.link.explanation, [targetedName, targetedSite]);
}
return newTabString("suggested.button", [targetedName]);
},
/**
* Renders the site's data (fills the HTML fragment).
*/
@ -140,9 +160,9 @@ Site.prototype = {
}
this.node.setAttribute("suggested", true);
let targetedSite = `<strong> ${this.link.targetedName} </strong>`;
let explanation = this._getSuggestedTileExplanation();
this._querySelector(".newtab-suggested").innerHTML =
`<div class='newtab-suggested-bounds'> ${newTabString("suggested.button", [targetedSite])} </div>`;
`<div class='newtab-suggested-bounds'> ${explanation} </div>`;
}
if (this.isPinned())

View File

@ -3,6 +3,14 @@
const PRELOAD_PREF = "browser.newtab.preload";
let suggestedLink = {
url: "http://example1.com/2",
imageURI: "data:image/png;base64,helloWORLD3",
title: "title2",
type: "affiliate",
frecent_sites: ["classroom.google.com", "codecademy.com", "elearning.ut.ac.id", "khanacademy.org", "learn.jquery.com", "teamtreehouse.com", "tutorialspoint.com", "udacity.com", "w3cschool.cc", "w3schools.com"]
};
gDirectorySource = "data:application/json," + JSON.stringify({
"enhanced": [{
url: "http://example.com/",
@ -16,21 +24,12 @@ gDirectorySource = "data:application/json," + JSON.stringify({
title: "title1",
type: "organic"
}],
"suggested": [{
url: "http://example1.com/2",
imageURI: "data:image/png;base64,helloWORLD3",
title: "title2",
type: "affiliate",
frecent_sites: ["test.com"]
}]
"suggested": [suggestedLink]
});
function runTests() {
let origGetFrecentSitesName = DirectoryLinksProvider.getFrecentSitesName;
DirectoryLinksProvider.getFrecentSitesName = () => "";
let origEnhanced = NewTabUtils.allPages.enhanced;
registerCleanupFunction(() => {
DirectoryLinksProvider.getFrecentSitesName = origGetFrecentSitesName;
Services.prefs.clearUserPref(PRELOAD_PREF);
NewTabUtils.allPages.enhanced = origEnhanced;
});
@ -46,6 +45,7 @@ function runTests() {
type: siteNode.getAttribute("type"),
enhanced: siteNode.querySelector(".enhanced-content").style.backgroundImage,
title: siteNode.querySelector(".newtab-title").textContent,
suggested: siteNode.querySelector(".newtab-suggested").innerHTML
};
}
@ -56,50 +56,56 @@ function runTests() {
yield addNewTabPageTab();
yield customizeNewTabPage("classic");
yield customizeNewTabPage("enhanced"); // Toggle enhanced off
let {type, enhanced, title} = getData(0);
let {type, enhanced, title, suggested} = getData(0);
isnot(type, "enhanced", "history link is not enhanced");
is(enhanced, "", "history link has no enhanced image");
is(title, "example.com");
is(suggested, "", "There is no suggested explanation");
is(getData(1), null, "there is only one link and it's a history link");
// Test with enhanced = true
yield addNewTabPageTab();
yield customizeNewTabPage("enhanced"); // Toggle enhanced on
({type, enhanced, title} = getData(0));
({type, enhanced, title, suggested} = getData(0));
is(type, "organic", "directory link is organic");
isnot(enhanced, "", "directory link has enhanced image");
is(title, "title1");
is(suggested, "", "There is no suggested explanation");
({type, enhanced, title} = getData(1));
({type, enhanced, title, suggested} = getData(1));
is(type, "enhanced", "history link is enhanced");
isnot(enhanced, "", "history link has enhanced image");
is(title, "title");
is(suggested, "", "There is no suggested explanation");
is(getData(2), null, "there are only 2 links, directory and enhanced history");
// Test with a pinned link
setPinnedLinks("-1");
yield addNewTabPageTab();
({type, enhanced, title} = getData(0));
({type, enhanced, title, suggested} = getData(0));
is(type, "enhanced", "pinned history link is enhanced");
isnot(enhanced, "", "pinned history link has enhanced image");
is(title, "title");
is(suggested, "", "There is no suggested explanation");
({type, enhanced, title} = getData(1));
({type, enhanced, title, suggested} = getData(1));
is(type, "organic", "directory link is organic");
isnot(enhanced, "", "directory link has enhanced image");
is(title, "title1");
is(suggested, "", "There is no suggested explanation");
is(getData(2), null, "directory link pushed out by pinned history link");
// Test pinned link with enhanced = false
yield addNewTabPageTab();
yield customizeNewTabPage("enhanced"); // Toggle enhanced off
({type, enhanced, title} = getData(0));
({type, enhanced, title, suggested} = getData(0));
isnot(type, "enhanced", "history link is not enhanced");
is(enhanced, "", "history link has no enhanced image");
is(title, "example.com");
is(suggested, "", "There is no suggested explanation");
is(getData(1), null, "directory link still pushed out by pinned history link");
@ -117,10 +123,11 @@ function runTests() {
// Test with enhanced = false
yield addNewTabPageTab();
({type, enhanced, title} = getData(0));
({type, enhanced, title, suggested} = getData(0));
isnot(type, "enhanced", "history link is not enhanced");
is(enhanced, "", "history link has no enhanced image");
is(title, "example.com");
is(suggested, "", "There is no suggested explanation");
isnot(getData(7), null, "there are 8 history links");
is(getData(8), null, "there are 8 history links");
@ -131,16 +138,52 @@ function runTests() {
yield customizeNewTabPage("enhanced");
// Suggested link was not enhanced by directory link with same domain
({type, enhanced, title} = getData(0));
({type, enhanced, title, suggested} = getData(0));
is(type, "affiliate", "suggested link is affiliate");
is(enhanced, "", "suggested link has no enhanced image");
is(title, "title2");
ok(suggested.indexOf("Suggested for <strong> Web Education </strong> visitors") > -1, "Suggested for 'Web Education'");
// Enhanced history link shows up second
({type, enhanced, title} = getData(1));
({type, enhanced, title, suggested} = getData(1));
is(type, "enhanced", "pinned history link is enhanced");
isnot(enhanced, "", "pinned history link has enhanced image");
is(title, "title");
is(suggested, "", "There is no suggested explanation");
is(getData(9), null, "there is a suggested link followed by an enhanced history link and the remaining history links");
// Test override category/adgroup name.
suggestedLink.adgroup_name = "Technology";
Services.prefs.setCharPref(PREF_NEWTAB_DIRECTORYSOURCE,
"data:application/json," + JSON.stringify({"suggested": [suggestedLink]}));
yield addNewTabPageTab();
({type, enhanced, title, suggested} = getData(0));
Cu.reportError("SUGGEST " + suggested);
ok(suggested.indexOf("Suggested for <strong> Technology </strong> visitors") > -1, "Suggested for 'Technology'");
// Test server provided explanation string.
suggestedLink.explanation = "Suggested for %1$S enthusiasts who visit sites like %2$S";
Services.prefs.setCharPref(PREF_NEWTAB_DIRECTORYSOURCE,
"data:application/json," + encodeURIComponent(JSON.stringify({"suggested": [suggestedLink]})));
yield addNewTabPageTab();
({type, enhanced, title, suggested} = getData(0));
Cu.reportError("SUGGEST " + suggested);
ok(suggested.indexOf("Suggested for <strong> Technology </strong> enthusiasts who visit sites like <strong> classroom.google.com </strong>") > -1, "Suggested for 'Technology' enthusiasts");
// Test server provided explanation string without category override.
delete suggestedLink.adgroup_name;
Services.prefs.setCharPref(PREF_NEWTAB_DIRECTORYSOURCE,
"data:application/json," + encodeURIComponent(JSON.stringify({"suggested": [suggestedLink]})));
yield addNewTabPageTab();
({type, enhanced, title, suggested} = getData(0));
Cu.reportError("SUGGEST " + suggested);
ok(suggested.indexOf("Suggested for <strong> Web Education </strong> enthusiasts who visit sites like <strong> classroom.google.com </strong>") > -1, "Suggested for 'Web Education' enthusiasts");
}

View File

@ -130,8 +130,10 @@ Below is an example directory source file::
],
"suggested": [
{
"adgroup_name": "open-source browser",
"bgColor": "#cae1f4",
"directoryId": 702,
"explanation": "Suggested for %1$S enthusiasts who visit sites like %2$S",
"frecent_sites": [
"addons.mozilla.org",
"air.mozilla.org",
@ -180,6 +182,11 @@ Suggested Link Object Extras
A suggested link has additional values:
- ``adgroup_name`` - string to override the hardcoded display name of the
triggering set of sites in Firefox.
- ``explanation`` - string to override the default explanation that appears
below a Suggested Tile. %1$S is replaced by the triggering adgroup name and
%2$S is replaced by the triggering site.
- ``frecent_sites`` - array of strings of the sites that can trigger showing a
Suggested Tile if the user has the site in one of the top 100 most-frecent
pages. Only preapproved array of strings that are hardcoded into the

View File

@ -9,6 +9,7 @@ this.EXPORTED_SYMBOLS = ["DirectoryLinksProvider"];
const Ci = Components.interfaces;
const Cc = Components.classes;
const Cu = Components.utils;
const ParserUtils = Cc["@mozilla.org/parserutils;1"].getService(Ci.nsIParserUtils);
Cu.importGlobalProperties(["XMLHttpRequest"]);
@ -617,7 +618,12 @@ let DirectoryLinksProvider = {
return;
}
link.targetedName = name;
let sanitizeFlags = ParserUtils.SanitizerCidEmbedsOnly |
ParserUtils.SanitizerDropForms |
ParserUtils.SanitizerDropNonCSSPresentation;
link.explanation = link.explanation ? ParserUtils.convertToPlainText(link.explanation, sanitizeFlags, 0) : "";
link.targetedName = ParserUtils.convertToPlainText(link.adgroup_name, sanitizeFlags, 0) || name;
link.lastVisitDate = rawLinks.suggested.length - position;
// We cache suggested tiles here but do not push any of them in the links list yet.

View File

@ -104,6 +104,16 @@ let suggestedTile4 = {
"sponsoredtarget.com"
]
}
let suggestedTile5 = {
url: "http://eviltile.com",
type: "affiliate",
lastVisitDate: 5,
explanation: "This is an evil tile <form><button formaction='javascript:alert(1)''>X</button></form> muhahaha",
adgroup_name: "WE ARE EVIL <link rel='import' href='test.svg'/>",
frecent_sites: [
"eviltarget.com"
]
}
let someOtherSite = {url: "http://someothersite.com", title: "Not_A_Suggested_Site"};
function getHttpHandler(path) {
@ -442,6 +452,7 @@ add_task(function test_suggestedLinksMap() {
for (let link of expected_data[site]) {
let linkCopy = JSON.parse(JSON.stringify(link));
linkCopy.targetedName = "testing map";
linkCopy.explanation = "";
isIdentical(suggestedLinksItr.next().value, linkCopy);
}
})
@ -1688,3 +1699,25 @@ add_task(function test_DirectoryLinksProvider_ClickRemoval() {
add_task(function test_DirectoryLinksProvider_anonymous() {
do_check_true(DirectoryLinksProvider._newXHR().mozAnon);
});
add_task(function test_sanitizeExplanation() {
// Note: this is a basic test to ensure we applied sanitization to the link explanation.
// Full testing for appropriate sanitization is done in parser/xml/test/unit/test_sanitizer.js.
let origGetFrecentSitesName = DirectoryLinksProvider.getFrecentSitesName;
DirectoryLinksProvider.getFrecentSitesName = () => "testing map";
let data = {"suggested": [suggestedTile5]};
let dataURI = 'data:application/json,' + encodeURIComponent(JSON.stringify(data));
yield promiseSetupDirectoryLinksProvider({linksURL: dataURI});
let links = yield fetchData();
let suggestedSites = [...DirectoryLinksProvider._suggestedLinks.keys()];
do_check_eq(suggestedSites.indexOf("eviltarget.com"), 0);
do_check_eq(suggestedSites.length, 1);
let suggestedLink = [...DirectoryLinksProvider._suggestedLinks.get(suggestedSites[0]).values()][0];
do_check_eq(suggestedLink.explanation, "This is an evil tile X muhahaha");
do_check_eq(suggestedLink.targetedName, "WE ARE EVIL ");
});