summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/bookmarks/test_bmindex.js
blob: c79da882821462f73c2c09c9e56c9461769ad5bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const NUM_BOOKMARKS = 20;
const NUM_SEPARATORS = 5;
const NUM_FOLDERS = 10;
const NUM_ITEMS = NUM_BOOKMARKS + NUM_SEPARATORS + NUM_FOLDERS;
const MIN_RAND = -5;
const MAX_RAND = 40;

async function check_contiguous_indexes(bookmarks) {
  var indexes = [];
  for (let bm of bookmarks) {
    let bmIndex = (await PlacesUtils.bookmarks.fetch(bm.guid)).index;
    info(`Index: ${bmIndex}\n`);
    info("Checking duplicates\n");
    Assert.ok(!indexes.includes(bmIndex));
    info(`Checking out of range, found ${bookmarks.length} items\n`);
    Assert.ok(bmIndex >= 0 && bmIndex < bookmarks.length);
    indexes.push(bmIndex);
  }
  info("Checking all valid indexes have been used\n");
  Assert.equal(indexes.length, bookmarks.length);
}

add_task(async function test_bookmarks_indexing() {
  let bookmarks = [];
  // Insert bookmarks with random indexes.
  for (let i = 0; bookmarks.length < NUM_BOOKMARKS; i++) {
    let randIndex = Math.round(
      MIN_RAND + Math.random() * (MAX_RAND - MIN_RAND)
    );
    try {
      let bm = await PlacesUtils.bookmarks.insert({
        index: randIndex,
        parentGuid: PlacesUtils.bookmarks.unfiledGuid,
        title: `Test bookmark ${i}`,
        url: `http://${i}.mozilla.org/`,
      });
      if (randIndex < -1) {
        do_throw("Creating a bookmark at an invalid index should throw");
      }
      bookmarks.push(bm);
    } catch (ex) {
      if (randIndex >= -1) {
        do_throw("Creating a bookmark at a valid index should not throw");
      }
    }
  }
  await check_contiguous_indexes(bookmarks);

  // Insert separators with random indexes.
  for (let i = 0; bookmarks.length < NUM_BOOKMARKS + NUM_SEPARATORS; i++) {
    let randIndex = Math.round(
      MIN_RAND + Math.random() * (MAX_RAND - MIN_RAND)
    );
    try {
      let bm = await PlacesUtils.bookmarks.insert({
        index: randIndex,
        parentGuid: PlacesUtils.bookmarks.unfiledGuid,
        type: PlacesUtils.bookmarks.TYPE_SEPARATOR,
      });
      if (randIndex < -1) {
        do_throw("Creating a separator at an invalid index should throw");
      }
      bookmarks.push(bm);
    } catch (ex) {
      if (randIndex >= -1) {
        do_throw("Creating a separator at a valid index should not throw");
      }
    }
  }
  await check_contiguous_indexes(bookmarks);

  // Insert folders with random indexes.
  for (let i = 0; bookmarks.length < NUM_ITEMS; i++) {
    let randIndex = Math.round(
      MIN_RAND + Math.random() * (MAX_RAND - MIN_RAND)
    );
    try {
      let bm = await PlacesUtils.bookmarks.insert({
        index: randIndex,
        parentGuid: PlacesUtils.bookmarks.unfiledGuid,
        title: `Test folder ${i}`,
        type: PlacesUtils.bookmarks.TYPE_FOLDER,
      });
      if (randIndex < -1) {
        do_throw("Creating a folder at an invalid index should throw");
      }
      bookmarks.push(bm);
    } catch (ex) {
      if (randIndex >= -1) {
        do_throw("Creating a folder at a valid index should not throw");
      }
    }
  }
  await check_contiguous_indexes(bookmarks);

  // Execute some random bookmark delete.
  for (let i = 0; i < Math.ceil(NUM_ITEMS / 4); i++) {
    let bm = bookmarks.splice(
      Math.floor(Math.random() * bookmarks.length),
      1
    )[0];
    info(`Removing item with guid ${bm.guid}\n`);
    await PlacesUtils.bookmarks.remove(bm);
  }
  await check_contiguous_indexes(bookmarks);

  // Execute some random bookmark move.  This will also try to move it to
  // invalid index values.
  for (let i = 0; i < Math.ceil(NUM_ITEMS / 4); i++) {
    let randIndex = Math.floor(Math.random() * bookmarks.length);
    let bm = bookmarks[randIndex];
    let newIndex = Math.round(MIN_RAND + Math.random() * (MAX_RAND - MIN_RAND));
    info(`Moving item with guid ${bm.guid} to index ${newIndex}\n`);
    try {
      bm.index = newIndex;
      await PlacesUtils.bookmarks.update(bm);
      if (newIndex < -1) {
        do_throw("Moving an item to a negative index should throw\n");
      }
    } catch (ex) {
      if (newIndex >= -1) {
        do_throw("Moving an item to a valid index should not throw\n");
      }
    }
  }
  await check_contiguous_indexes(bookmarks);
});