From fd14f080e835bfd3dd39678912f85ac2f7292582 Mon Sep 17 00:00:00 2001 From: tzhuang Date: Wed, 18 Dec 2013 18:52:19 +0800 Subject: [PATCH] Bug 942762 - Update label of bookmark on homescreen when user update bookmark name in browser --- apps/homescreen/js/page.js | 7 +- .../test/marionette/fixtures/sample.html | 11 ++ .../test/marionette/install_bookmark_test.js | 111 ++++++++++++++++++ .../test/marionette/lib/bookmark_editor.js | 44 +++++++ .../homescreen/test/marionette/lib/browser.js | 78 ++++++++++++ .../test/marionette/lib/homescreen.js | 31 +++++ apps/homescreen/test/marionette/lib/server.js | 45 +++++++ .../test/marionette/lib/server_child.js | 43 +++++++ 8 files changed, 366 insertions(+), 4 deletions(-) create mode 100644 apps/homescreen/test/marionette/fixtures/sample.html create mode 100644 apps/homescreen/test/marionette/install_bookmark_test.js create mode 100644 apps/homescreen/test/marionette/lib/bookmark_editor.js create mode 100644 apps/homescreen/test/marionette/lib/browser.js create mode 100644 apps/homescreen/test/marionette/lib/server.js create mode 100644 apps/homescreen/test/marionette/lib/server_child.js diff --git a/apps/homescreen/js/page.js b/apps/homescreen/js/page.js index cf60124e906d..485cf0207d99 100644 --- a/apps/homescreen/js/page.js +++ b/apps/homescreen/js/page.js @@ -494,8 +494,7 @@ Icon.prototype = { */ translate: function icon_translate() { var descriptor = this.descriptor; - if (descriptor.type === GridItemsFactory.TYPE.BOOKMARK || - descriptor.customName) + if (descriptor.customName) return; var app = this.app; @@ -506,13 +505,13 @@ Icon.prototype = { if (!manifest) return; - var localizedName; + var localizedName = manifest.name; if (descriptor.type === GridItemsFactory.TYPE.COLLECTION) { // try to translate, but fall back to current name // (translation might fail for custom collection name) localizedName = navigator.mozL10n.get(manifest.name) || manifest.name; - } else { + } else if (descriptor.type !== GridItemsFactory.TYPE.BOOKMARK) { var iconsAndNameHolder = manifest; var entryPoint = descriptor.entry_point; if (entryPoint) diff --git a/apps/homescreen/test/marionette/fixtures/sample.html b/apps/homescreen/test/marionette/fixtures/sample.html new file mode 100644 index 000000000000..ceb2f63c9865 --- /dev/null +++ b/apps/homescreen/test/marionette/fixtures/sample.html @@ -0,0 +1,11 @@ + + + + + Sample page + + +

Mozilla

+
We are global community with a common purpose
+ + diff --git a/apps/homescreen/test/marionette/install_bookmark_test.js b/apps/homescreen/test/marionette/install_bookmark_test.js new file mode 100644 index 000000000000..567c10a92828 --- /dev/null +++ b/apps/homescreen/test/marionette/install_bookmark_test.js @@ -0,0 +1,111 @@ +'use strict'; + +var assert = require('assert'); +var Browser = require('./lib/browser'); +var Homescreen = require('./lib/homescreen'); +var Server = require('./lib/server'); + +marionette('Install bookmark on homescreen', function() { + var client = marionette.client({ + settings: { + // disable keyboard ftu because it blocks our display + 'keyboard.ftu.enabled': false + } + }); + var browser; + var homescreen; + var server; + + suiteSetup(function(done) { + Server.create(function(err, _server) { + server = _server; + done(); + }); + }); + + suiteTeardown(function() { + server.stop(); + }); + + setup(function() { + browser = new Browser(client); + browser.launch(); + client.helper.waitForElement('body.loaded'); + }); + + suite(' > Navigate to sample.html and bookmark it on homescreen', function() { + var url; + var expectedTitle = 'Sample page'; + + setup(function() { + var notifToaster; + homescreen = new Homescreen(client); + + url = server.url('sample.html'); + + // Running tests with B2G desktop on Linux, a 'Download complete' + // notification-toaster will pop up and make tests failed + client.switchToFrame(); + notifToaster = client.findElement('#notification-toaster'); + if (notifToaster.displayed()) { + // Bug 952377: client.helper.waitForElementToDisappear(notifToaster) + // will failed and got timeout. + // (notifToaster.displayed() is always true) + // So we workaround this to wait for .displayed get removed + // from notifToaster + client.helper.waitFor(function() { + return notifToaster.getAttribute('class').indexOf('displayed') < 0; + }); + } + browser.backToApp(); + + browser.searchBar.sendKeys(url); + browser.searchButton.click(); + // this will fail on linux because a downloaded notification poped up + client.helper.waitForElement(browser.bookmarkButton).click(); + client.helper.waitForElement(browser.addToHomeButton).click(); + homescreen.switchToBookmarkEditorFrame(); + homescreen.bookmarkEditor.bookmarkAddButton.click(); + }); + + test(' sample.html is on homescreen with expected title', + function() { + client.switchToFrame(); + homescreen.launch(); + assert.ok(homescreen.getHomescreenIcon(expectedTitle) != null); + assert.ok( + homescreen.getLabelOfBookmark(expectedTitle).text(), + expectedTitle); + }); + + suite(' > Change the title of bookmark on homescreen to new expected title', + function() { + var newExpectedTitle = 'New Title'; + + setup(function() { + homescreen.bookmarkEditor.waitForDisappearance(); + browser.backToApp(); + client.helper.waitForElement(browser.bookmarkButton).click(); + client.helper.waitForElement(browser.addToHomeButton).click(); + homescreen.switchToBookmarkEditorFrame(); + homescreen.bookmarkEditor.bookmarkTitleField.clear(); + homescreen.bookmarkEditor + .bookmarkTitleField.sendKeys(newExpectedTitle); + // tap head element to make keyboard away + homescreen.bookmarkEditor.bookmarkEntrySheetHead.tap(); + homescreen.bookmarkEditor.bookmarkAddButton.click(); + }); + + test(' And we change the title of it', function() { + client.switchToFrame(); + homescreen.launch(); + // aria-label won't change after we change bookmark title, + // so we select the element using previous title + assert.ok(homescreen.getHomescreenIcon(expectedTitle) != null); + assert.ok( + homescreen.getLabelOfBookmark(expectedTitle).text(), + newExpectedTitle); + }); + }); + }); +}); diff --git a/apps/homescreen/test/marionette/lib/bookmark_editor.js b/apps/homescreen/test/marionette/lib/bookmark_editor.js new file mode 100644 index 000000000000..630245fed6c5 --- /dev/null +++ b/apps/homescreen/test/marionette/lib/bookmark_editor.js @@ -0,0 +1,44 @@ +var BookmarkEditor = function b_ctor(client) { + this.client = client; +}; + +BookmarkEditor.Selectors = { + 'mozbrowser': '.inline-activity.active > iframe[mozbrowser]', + 'bookmarkAddButton': '#button-bookmark-add', + 'bookmarkTitleField': '#bookmark-title', + 'bookmarkEntrySheetHead': '#bookmark-entry-sheet > header > h1' +}; + +BookmarkEditor.prototype = { + get bookmarkAddButton() { + return this.client.findElement( + BookmarkEditor.Selectors['bookmarkAddButton']); + }, + + get bookmarkTitleField() { + return this.client.findElement( + BookmarkEditor.Selectors['bookmarkTitleField']); + }, + + get bookmarkEntrySheetHead() { + return this.client.findElement( + BookmarkEditor.Selectors['bookmarkEntrySheetHead']); + }, + + get currentTabFrame() { + return this.client.findElement( + BookmarkEditor.Selectors['mozbrowser']); + }, + + backToApp: function() { + this.client.switchToFrame(); + this.client.switchToFrame(this.currentTabFrame); + }, + + waitForDisappearance: function() { + this.client.switchToFrame(); + this.client.helper.waitForElementToDisappear(this.currentTabFrame); + } +}; + +module.exports = BookmarkEditor; diff --git a/apps/homescreen/test/marionette/lib/browser.js b/apps/homescreen/test/marionette/lib/browser.js new file mode 100644 index 000000000000..3e897ac5c71a --- /dev/null +++ b/apps/homescreen/test/marionette/lib/browser.js @@ -0,0 +1,78 @@ +/** + * Abstraction around browser app. + * @constructor + * @param {Marionette.Client} client for operations. + */ +function Browser(client) { + this.client = client; +} + +/** + * @type {string} Origin of browser app. + */ +Browser.URL = 'app://browser.gaiamobile.org'; + +Browser.Selectors = { + 'searchBar': '#url-input', + 'searchButton': '#url-button', + 'mozbrowser': 'iframe[mozbrowser]', + 'bookmarkButton': '#bookmark-button', + 'addToHomeButton': '#bookmark-menu-add-home' +}; + +/** + * @private + * @param {Marionette.Client} client for selector. + * @param {String} name of selector [its a key in Browser.Selectors]. + */ +function findElement(client, name) { + return client.findElement(Browser.Selectors[name]); +} + +Browser.prototype = { + get searchBar() { + return findElement(this.client, 'searchBar'); + }, + + get searchButton() { + return findElement(this.client, 'searchButton'); + }, + + get bookmarkButton() { + return findElement(this.client, 'bookmarkButton'); + }, + + get addToHomeButton() { + return findElement(this.client, 'addToHomeButton'); + }, + + /** + * Finds iframe of current running tab. + */ + currentTabFrame: function() { + // being really lazy right now and just finding first mozbrowser + return this.client.findElement(Browser.Selectors.mozbrowser); + }, + + /** + * Launches browser app and focuses on frame. + */ + launch: function() { + this.client.apps.launch(Browser.URL); + this.client.apps.switchToApp(Browser.URL); + }, + + close: function() { + this.client.apps.close(Browser.URL); + }, + + /** + * Back to Browser app frame + */ + backToApp: function() { + this.client.switchToFrame(); + this.client.apps.switchToApp(Browser.URL); + } +}; + +module.exports = Browser; diff --git a/apps/homescreen/test/marionette/lib/homescreen.js b/apps/homescreen/test/marionette/lib/homescreen.js index 1ea0d6251955..25c35efcffc9 100644 --- a/apps/homescreen/test/marionette/lib/homescreen.js +++ b/apps/homescreen/test/marionette/lib/homescreen.js @@ -7,6 +7,8 @@ function Homescreen(client) { this.client = client; } +var BookmarkEditor = require('./bookmark_editor'); + /** * @type String Origin of Homescreen app */ @@ -17,6 +19,13 @@ Homescreen.Selectors = { }; Homescreen.prototype = { + get bookmarkEditor() { + if (!this._bookmarkEditor) { + this._bookmarkEditor = new BookmarkEditor(this.client); + } + return this._bookmarkEditor; + }, + /** * Launches Homescreen app and focuses on frame. */ @@ -33,6 +42,28 @@ Homescreen.prototype = { var selectors = Homescreen.Selectors; this.client.helper.waitForElement(selectors.searchBar) .click(); + }, + + close: function() { + this.client.apps.close(Homescreen.URL); + }, + + backToApp: function() { + this.client.switchToFrame(); + this.client.apps.switchToApp(Homescreen.URL); + }, + + getHomescreenIcon: function(title) { + return this.client.findElement('li.icon[aria-label="' + title + '"]'); + }, + + getLabelOfBookmark: function(title) { + return this.client.findElement( + 'li.icon[aria-label="' + title + '"] span.labelWrapper > span'); + }, + + switchToBookmarkEditorFrame: function() { + this.bookmarkEditor.backToApp(); } }; diff --git a/apps/homescreen/test/marionette/lib/server.js b/apps/homescreen/test/marionette/lib/server.js new file mode 100644 index 000000000000..2ef8780b9d26 --- /dev/null +++ b/apps/homescreen/test/marionette/lib/server.js @@ -0,0 +1,45 @@ +function Server(port, child) { + this.port = port; + this.child = child; +} + +Server.prototype = { + /** + * Formats given input with localhost and port. + * + * @param {String} path part of url. + * @return {String} url of location. + */ + url: function(path) { + return 'http://localhost:' + this.port + '/' + path; + }, + + /** + * Sends signal to stop child process and stop server. + */ + stop: function() { + this.child.send('stop'); + this.child.kill(); + } +}; + +/** + * Spawn the child process where the http server lives. + * + * @param {Function} callback [Error err, Server server]. + */ +function create(callback) { + var fork = require('child_process').fork; + var child = fork(__dirname + '/server_child.js'); + + // wait for start message ['start', PORT_NUMBER]. + child.on('message', function(data) { + if (Array.isArray(data) && data[0] === 'start') { + callback(null, new Server(data[1], child)); + } + }); +} + +Server.create = create; + +module.exports = Server; diff --git a/apps/homescreen/test/marionette/lib/server_child.js b/apps/homescreen/test/marionette/lib/server_child.js new file mode 100644 index 000000000000..c1dc4155181b --- /dev/null +++ b/apps/homescreen/test/marionette/lib/server_child.js @@ -0,0 +1,43 @@ +var http = require('http'), + emptyPort = require('empty-port'), + static = require('node-static'); + + +var Server = { + /** + * Http server running in this process. + */ + http: null, + + stop: function() { + if (this.http) { + this.http.kill(); + } + }, + + start: function(port) { + // using node-static for now we can do fancy stuff in the future. + var file = new static.Server(__dirname + '/../fixtures/'); + this.http = http.createServer(function(req, res) { + req.addListener('end', function() { + // hand off request to node-static + file.serve(req, res); + }).resume(); + }).listen(port); + } +}; + +// figure out which port we are on +emptyPort({}, function(err, port) { + Server.start(port); + process.send(['start', port]); +}); + +// handle process messages +process.on('message', function(data) { + switch (data) { + case 'stop': + Server.stop(); + break; + } +});