Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Bug 1078202 - Switch topsites customiser to write places directly. r=…
Browse files Browse the repository at this point in the history
…kgrandon
  • Loading branch information
daleharvey committed Oct 10, 2014
1 parent f81d4c5 commit 29c271e
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 88 deletions.
50 changes: 25 additions & 25 deletions apps/operatorvariant/js/customizers/topsites_customizer.js
Expand Up @@ -5,34 +5,34 @@
var TopSitesCustomizer = (function() {
Customizer.call(this, 'topsites', 'json');

this.set = function(aDatas) {
try {
if (!aDatas) {
return;
}
var settings = navigator.mozSettings;
if (!settings) {
console.error('TopSitesCustomizer. Settings is not available');
return;
}

var settingsValue = {};

for (var key in aDatas) {
settingsValue[key] = aDatas[key];
}

if (Object.keys(settingsValue).length) {
settings.createLock().set({
'operatorResources.data.topsites': settingsValue
});
}
this.set = function(topSiteConfig) {

} catch (e) {
console.error('TopSitesCustomizer. Error recovering datas. ' +
'We will use default values. ' + e);
if (!topSiteConfig ||
!topSiteConfig.topSites ||
!topSiteConfig.topSites.length) {
return;
}

this.getStore('places').then(store => {
topSiteConfig.topSites.forEach(site => {
store.get(site.url).then(place => {
if (!place) {
site.frecency = -1;
store.put(site, site.url);
}
});
});
});
};

this.getStore = function(name) {
return new Promise(resolve => {
navigator.getDataStores(name).then(stores => {
resolve(stores[0]);
});
});
};

});

var topSitesCustomizer = new TopSitesCustomizer();
Expand Down
4 changes: 4 additions & 0 deletions apps/operatorvariant/manifest.webapp
Expand Up @@ -22,6 +22,10 @@
"bookmarks_store": {
"readonly": false,
"description": "Stores bookmarks"
},
"places": {
"readonly": false,
"description": "Stores data about browsing history."
}
}
}
52 changes: 52 additions & 0 deletions apps/operatorvariant/test/unit/topsite_customizer_test.js
@@ -0,0 +1,52 @@
/* global MockNavigatorDatastore */
/* global MockDatastore */
/* global topSitesCustomizer */

'use strict';

requireApp('operatorvariant/js/customizers/customizer.js');
requireApp('operatorvariant/js/customizers/topsites_customizer.js');
require('/shared/test/unit/mocks/mock_navigator_datastore.js');

suite('TopSitesCustomizer >', function() {
var realDatastore = null;

suiteSetup(function() {
navigator.getDataStores = MockNavigatorDatastore.getDataStores;
});

suiteTeardown(function() {
navigator.getDataStores = realDatastore;
});

teardown(function() {
MockDatastore._inError = false;
MockDatastore._records = Object.create(null);
});

test('First run with valid SIM. Set places', function(done) {

var url = 'http://example.org';
var title = 'Example';
var tile = 'atile';
var data = {
topSites: [{
url: url,
title: title,
tile: tile,
}]
};

MockDatastore.addEventListener('change', function() {
assert.deepEqual(MockDatastore._records[url], {
url: url,
title: title,
tile: tile,
frecency: -1
}, 'Arguments not equal');
done();
});
topSitesCustomizer.simPresentOnFirstBoot = true;
topSitesCustomizer.set(data);
});
});
23 changes: 4 additions & 19 deletions apps/search/js/providers/places.js
Expand Up @@ -348,30 +348,15 @@

saveSites: function(sites) {
return Promise.all(sites.map(site => {
site.frecency = 0;
site.frecency = -2;
return this.persistStore.addPlace(site);
}));
},

preloadTopSites: function() {

var TOPSITE_KEY = 'operatorResources.data.topsites';

// Switch the existing format of sim topsites into the current format
return new Promise(resolve => {
// Attempt to load top sites from sim variant data
var request = navigator.mozSettings.createLock().get(TOPSITE_KEY);
request.onsuccess = () => {
var result = request.result[TOPSITE_KEY];
if (result && result.topSites) {
return this.saveSites(result.topSites).then(resolve);
}

// No sim variant data found, load default build top sites
return LazyLoader.getJSON('/js/inittopsites.json').then(sites => {
return this.saveSites(sites).then(resolve);
});
};
// load default build top sites
return LazyLoader.getJSON('/js/inittopsites.json').then(sites => {
return this.saveSites(sites);
});
},

Expand Down
71 changes: 27 additions & 44 deletions apps/search/test/marionette/browser_test.js
@@ -1,18 +1,31 @@
'use strict';

/* global __dirname */

var Home2 = require('../../../verticalhome/test/marionette/lib/home2');
var System = require('../../../system/test/marionette/lib/system');
var Search = require('./lib/search');
var Server = require('../../../../shared/test/integration/server');
var Rocketbar = require('../../../system/test/marionette/lib/rocketbar.js');

var assert = require('chai').assert;

marionette('Browser test', function() {

var client = marionette.client(Home2.clientOptions);
var search, system;
var search, system, server, home, rocketbar;

suiteSetup(function(done) {
Server.create(__dirname + '/fixtures/', function(err, _server) {
server = _server;
done();
});
});

setup(function() {
home = new Home2(client);
search = new Search(client);
rocketbar = new Rocketbar(client);
system = new System(client);
system.waitForStartup();
search.removeGeolocationPermission();
Expand All @@ -27,59 +40,29 @@ marionette('Browser test', function() {
});
});

test('Ensure sim variant preloaded sites exist', function() {

client.executeAsyncScript(function() {
var settings = window.wrappedJSObject.navigator.mozSettings;
var result = settings.createLock().set({
'operatorResources.data.topsites': {
'topSites': [{
url: 'http://example1.org',
title: 'Example1'
}, {
url: 'http://example2.org',
title: 'Example2'
}, {
url: 'http://example3.org',
title: 'Example3'
}]
}
});
result.onsuccess = function() {
marionetteScriptFinished();
};
});
test.skip('Ensure fallback to url when no place title', function() {

client.apps.launch(Search.URL);
client.apps.switchToApp(Search.URL);
var url = server.url('notitle.html');

client.waitFor(function() {
return search.getTopSites().length == 3;
});
});
home.waitForLaunch();
home.focusRocketBar();
search.triggerFirstRun(rocketbar);

test('Ensure fallback to url when no place title', function() {

client.executeAsyncScript(function() {
var settings = window.wrappedJSObject.navigator.mozSettings;
var result = settings.createLock().set({
'operatorResources.data.topsites': {
'topSites': [{url: 'http://example1.org'}]
}
});
result.onsuccess = function() {
marionetteScriptFinished();
};
});
// Input a url and press enter to visit
rocketbar.enterText(url + '\uE006');
rocketbar.switchToBrowserFrame(url);

client.switchToFrame();
home.pressHomeButton();

client.apps.launch(Search.URL);
client.apps.switchToApp(Search.URL);

client.waitFor(function() {
return search.getTopSites().length == 1;
return search.getTopSites().length == 3;
});

var topSite = search.getTopSites()[0];
assert.equal(topSite.text(), 'http://example1.org');
assert.equal(topSite.text(), url);
});
});
10 changes: 10 additions & 0 deletions apps/search/test/marionette/fixtures/notitle.html
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<header><h1>Mozilla</h1></header>
<section>We are global community with a common purpose</section>
</body>
</html>

0 comments on commit 29c271e

Please sign in to comment.