addon-sdk/source/test/addons/places/tests/test-places-host.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     4 'use strict';
     6 module.metadata = {
     7   'engines': {
     8     'Firefox': '*'
     9   }
    10 };
    12 const { Cc, Ci } = require('chrome');
    13 const { defer, all } = require('sdk/core/promise');
    14 const { setTimeout } = require('sdk/timers');
    15 const { newURI } = require('sdk/url/utils');
    16 const { send } = require('sdk/addon/events');
    17 const { set } = require('sdk/preferences/service');
    18 const { before, after } = require('sdk/test/utils');
    20 require('sdk/places/host/host-bookmarks');
    21 require('sdk/places/host/host-tags');
    22 require('sdk/places/host/host-query');
    23 const {
    24   invalidResolve, invalidReject, createTree,
    25   compareWithHost, createBookmark, createBookmarkTree, resetPlaces
    26 } = require('../places-helper');
    28 const bmsrv = Cc['@mozilla.org/browser/nav-bookmarks-service;1'].
    29                     getService(Ci.nsINavBookmarksService);
    30 const hsrv = Cc['@mozilla.org/browser/nav-history-service;1'].
    31               getService(Ci.nsINavHistoryService);
    32 const tagsrv = Cc['@mozilla.org/browser/tagging-service;1'].
    33               getService(Ci.nsITaggingService);
    35 exports.testBookmarksCreate = function (assert, done) {
    36   let items = [{
    37     title: 'my title',
    38     url: 'http://test-places-host.com/testBookmarksCreate/',
    39     tags: ['some', 'tags', 'yeah'],
    40     type: 'bookmark'
    41   }, {
    42     title: 'my folder',
    43     type: 'group',
    44     group: bmsrv.bookmarksMenuFolder
    45   }, {
    46     type: 'separator',
    47     group: bmsrv.unfiledBookmarksFolder
    48   }];
    50   all(items.map(function (item) {
    51     return send('sdk-places-bookmarks-create', item).then(function (data) {
    52       compareWithHost(assert, data);
    53     }, invalidReject(assert));
    54   })).then(function () {
    55     done();
    56   }, invalidReject(assert));
    57 };
    59 exports.testBookmarksCreateFail = function (assert, done) {
    60   let items = [{
    61     title: 'my title',
    62     url: 'not-a-url',
    63     type: 'bookmark'
    64   }, {
    65     type: 'group',
    66     group: bmsrv.bookmarksMenuFolder
    67   }, {
    68     group: bmsrv.unfiledBookmarksFolder
    69   }];
    70   all(items.map(function (item) {
    71     return send('sdk-places-bookmarks-create', item).then(null, function (reason) {
    72       assert.ok(reason, 'bookmark create should fail');
    73     });
    74   })).then(done);
    75 };
    77 exports.testBookmarkLastUpdated = function (assert, done) {
    78   let timestamp;
    79   let item;
    80   createBookmark({
    81     url: 'http://test-places-host.com/testBookmarkLastUpdated'
    82   }).then(function (data) {
    83     item = data;
    84     timestamp = item.updated;
    85     return send('sdk-places-bookmarks-last-updated', { id: item.id });
    86   }).then(function (updated) {
    87     let { resolve, promise } = defer();
    88     assert.equal(timestamp, updated, 'should return last updated time');
    89     item.title = 'updated mozilla';
    90     setTimeout(() => {
    91       resolve(send('sdk-places-bookmarks-save', item));
    92     }, 100);
    93     return promise;
    94   }).then(function (data) {
    95     assert.ok(data.updated > timestamp, 'time has elapsed and updated the updated property');
    96     done();
    97   });
    98 };
   100 exports.testBookmarkRemove = function (assert, done) {
   101   let id;
   102   createBookmark({
   103     url: 'http://test-places-host.com/testBookmarkRemove/'
   104   }).then(function (data) {
   105     id = data.id;
   106     compareWithHost(assert, data); // ensure bookmark exists
   107     bmsrv.getItemTitle(id); // does not throw an error
   108     return send('sdk-places-bookmarks-remove', data);
   109   }).then(function () {
   110     assert.throws(function () {
   111       bmsrv.getItemTitle(id);
   112     }, 'item should no longer exist');
   113     done();
   114   }, assert.fail);
   115 };
   117 exports.testBookmarkGet = function (assert, done) {
   118   let bookmark;
   119   createBookmark({
   120     url: 'http://test-places-host.com/testBookmarkGet/'
   121   }).then(function (data) {
   122     bookmark = data;
   123     return send('sdk-places-bookmarks-get', { id: data.id });
   124   }).then(function (data) {
   125     'title url index group updated type tags'.split(' ').map(function (prop) {
   126       if (prop === 'tags') {
   127         for (let tag of bookmark.tags) {
   128           assert.ok(~data.tags.indexOf(tag),
   129             'correctly fetched tag ' + tag);
   130         }
   131         assert.equal(bookmark.tags.length, data.tags.length,
   132           'same amount of tags');
   133       }
   134       else
   135         assert.equal(bookmark[prop], data[prop], 'correctly fetched ' + prop);
   136     });
   137     done();
   138   });
   139 };
   141 exports.testTagsTag = function (assert, done) {
   142   let url;
   143   createBookmark({
   144     url: 'http://test-places-host.com/testTagsTag/',
   145   }).then(function (data) {
   146     url = data.url;
   147     return send('sdk-places-tags-tag', {
   148       url: data.url, tags: ['mozzerella', 'foxfire']
   149     });
   150   }).then(function () {
   151     let tags = tagsrv.getTagsForURI(newURI(url));
   152     assert.ok(~tags.indexOf('mozzerella'), 'first tag found');
   153     assert.ok(~tags.indexOf('foxfire'), 'second tag found');
   154     assert.ok(~tags.indexOf('firefox'), 'default tag found');
   155     assert.equal(tags.length, 3, 'no extra tags');
   156     done();
   157   });
   158 };
   160 exports.testTagsUntag = function (assert, done) {
   161   let item;
   162   createBookmark({
   163     url: 'http://test-places-host.com/testTagsUntag/',
   164     tags: ['tag1', 'tag2', 'tag3']
   165   }).then(data => {
   166     item = data;
   167     return send('sdk-places-tags-untag', {
   168       url: item.url,
   169       tags: ['tag2', 'firefox']
   170     });
   171   }).then(function () {
   172     let tags = tagsrv.getTagsForURI(newURI(item.url));
   173     assert.ok(~tags.indexOf('tag1'), 'first tag persisted');
   174     assert.ok(~tags.indexOf('tag3'), 'second tag persisted');
   175     assert.ok(!~tags.indexOf('firefox'), 'first tag removed');
   176     assert.ok(!~tags.indexOf('tag2'), 'second tag removed');
   177     assert.equal(tags.length, 2, 'no extra tags');
   178     done();
   179   });
   180 };
   182 exports.testTagsGetURLsByTag = function (assert, done) {
   183   let item;
   184   createBookmark({
   185     url: 'http://test-places-host.com/testTagsGetURLsByTag/'
   186   }).then(function (data) {
   187     item = data;
   188     return send('sdk-places-tags-get-urls-by-tag', {
   189       tag: 'firefox'
   190     });
   191   }).then(function(urls) {
   192     assert.equal(item.url, urls[0], 'returned correct url');
   193     assert.equal(urls.length, 1, 'returned only one url');
   194     done();
   195   });
   196 };
   198 exports.testTagsGetTagsByURL = function (assert, done) {
   199   let item;
   200   createBookmark({
   201     url: 'http://test-places-host.com/testTagsGetURLsByTag/',
   202     tags: ['firefox', 'mozilla', 'metal']
   203   }).then(function (data) {
   204     item = data;
   205     return send('sdk-places-tags-get-tags-by-url', {
   206       url: data.url,
   207     });
   208   }).then(function(tags) {
   209     assert.ok(~tags.indexOf('firefox'), 'returned first tag');
   210     assert.ok(~tags.indexOf('mozilla'), 'returned second tag');
   211     assert.ok(~tags.indexOf('metal'), 'returned third tag');
   212     assert.equal(tags.length, 3, 'returned all tags');
   213     done();
   214   });
   215 };
   217 exports.testHostQuery = function (assert, done) {
   218   all([
   219     createBookmark({
   220       url: 'http://firefox.com/testHostQuery/',
   221       tags: ['firefox', 'mozilla']
   222     }),
   223     createBookmark({
   224       url: 'http://mozilla.com/testHostQuery/',
   225       tags: ['mozilla']
   226     }),
   227     createBookmark({ url: 'http://thunderbird.com/testHostQuery/' })
   228   ]).then(data => {
   229     return send('sdk-places-query', {
   230       queries: { tags: ['mozilla'] },
   231       options: { sortingMode: 6, queryType: 1 } // sort by URI ascending, bookmarks only
   232     });
   233   }).then(results => {
   234     assert.equal(results.length, 2, 'should only return two');
   235     assert.equal(results[0].url,
   236       'http://mozilla.com/testHostQuery/', 'is sorted by URI asc');
   237     return send('sdk-places-query', {
   238       queries: { tags: ['mozilla'] },
   239       options: { sortingMode: 5, queryType: 1 } // sort by URI descending, bookmarks only
   240     });
   241   }).then(results => {
   242     assert.equal(results.length, 2, 'should only return two');
   243     assert.equal(results[0].url,
   244       'http://firefox.com/testHostQuery/', 'is sorted by URI desc');
   245     done();
   246   });
   247 };
   249 exports.testHostMultiQuery = function (assert, done) {
   250   all([
   251     createBookmark({
   252       url: 'http://firefox.com/testHostMultiQuery/',
   253       tags: ['firefox', 'mozilla']
   254     }),
   255     createBookmark({
   256       url: 'http://mozilla.com/testHostMultiQuery/',
   257       tags: ['mozilla']
   258     }),
   259     createBookmark({ url: 'http://thunderbird.com/testHostMultiQuery/' })
   260   ]).then(data => {
   261     return send('sdk-places-query', {
   262       queries: [{ tags: ['firefox'] }, { uri: 'http://thunderbird.com/testHostMultiQuery/' }],
   263       options: { sortingMode: 5, queryType: 1 } // sort by URI descending, bookmarks only
   264     });
   265   }).then(results => {
   266     assert.equal(results.length, 2, 'should return 2 results ORing queries');
   267     assert.equal(results[0].url,
   268       'http://firefox.com/testHostMultiQuery/', 'should match URL or tag');
   269     assert.equal(results[1].url,
   270       'http://thunderbird.com/testHostMultiQuery/', 'should match URL or tag');
   271     return send('sdk-places-query', {
   272       queries: [{ tags: ['firefox'], url: 'http://mozilla.com/testHostMultiQuery/' }],
   273       options: { sortingMode: 5, queryType: 1 } // sort by URI descending, bookmarks only
   274     });
   275   }).then(results => {
   276     assert.equal(results.length, 0, 'query props should be AND\'d');
   277     done();
   278   });
   279 };
   281 exports.testGetAllBookmarks = function (assert, done) {
   282   createBookmarkTree().then(() => {
   283     return send('sdk-places-bookmarks-get-all', {});
   284   }).then(res => {
   285     assert.equal(res.length, 8, 'all bookmarks returned');
   286     done();
   287   }, assert.fail);
   288 };
   290 exports.testGetAllChildren = function (assert, done) {
   291   createBookmarkTree().then(results => {
   292     return send('sdk-places-bookmarks-get-children', {
   293       id: results.filter(({title}) => title === 'mozgroup')[0].id
   294     });
   295   }).then(results => {
   296     assert.equal(results.length, 5,
   297       'should return all children and folders at a single depth');
   298     done();
   299   });
   300 };
   302 before(exports, (name, assert, done) => resetPlaces(done));
   303 after(exports, (name, assert, done) => resetPlaces(done));

mercurial