summaryrefslogtreecommitdiffstats
path: root/toolkit/profile/xpcshell/test_remove.js
blob: 8b5025d6123dc6964190acff533b25155a68dcad (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/*
 * Tests adding and removing functions correctly.
 */

function compareLists(service, knownProfiles) {
  Assert.equal(
    service.profileCount,
    knownProfiles.length,
    "profileCount should be correct."
  );
  let serviceProfiles = Array.from(service.profiles);
  Assert.equal(
    serviceProfiles.length,
    knownProfiles.length,
    "Enumerator length should be correct."
  );

  for (let i = 0; i < knownProfiles.length; i++) {
    // Cannot use strictEqual here, it attempts to print out a string
    // representation of the profile objects and on some platforms that recurses
    // infinitely.
    Assert.ok(
      serviceProfiles[i] === knownProfiles[i],
      `Should have the right profile in position ${i}.`
    );
  }
}

function removeProfile(profiles, position) {
  dump(`Removing profile in position ${position}.`);
  Assert.greaterOrEqual(position, 0, "Should be removing a valid position.");
  Assert.less(
    position,
    profiles.length,
    "Should be removing a valid position."
  );

  let last = profiles.pop();

  if (profiles.length == position) {
    // We were asked to remove the last profile.
    last.remove(false);
    return;
  }

  profiles[position].remove(false);
  profiles[position] = last;
}

add_task(async () => {
  let service = getProfileService();
  let profiles = [];
  compareLists(service, profiles);

  profiles.push(service.createProfile(null, "profile1"));
  profiles.push(service.createProfile(null, "profile2"));
  profiles.push(service.createProfile(null, "profile3"));
  profiles.push(service.createProfile(null, "profile4"));
  profiles.push(service.createProfile(null, "profile5"));
  profiles.push(service.createProfile(null, "profile6"));
  profiles.push(service.createProfile(null, "profile7"));
  profiles.push(service.createProfile(null, "profile8"));
  profiles.push(service.createProfile(null, "profile9"));
  compareLists(service, profiles);

  // Test removing the first profile.
  removeProfile(profiles, 0);
  compareLists(service, profiles);

  // And the last profile.
  removeProfile(profiles, profiles.length - 1);
  compareLists(service, profiles);

  // Last but one...
  removeProfile(profiles, profiles.length - 2);
  compareLists(service, profiles);

  // Second one...
  removeProfile(profiles, 1);
  compareLists(service, profiles);

  // Something in the middle.
  removeProfile(profiles, 2);
  compareLists(service, profiles);

  let expectedNames = ["profile9", "profile7", "profile5", "profile4"];

  let serviceProfiles = Array.from(service.profiles);
  for (let i = 0; i < expectedNames.length; i++) {
    Assert.equal(serviceProfiles[i].name, expectedNames[i]);
  }

  removeProfile(profiles, 0);
  removeProfile(profiles, 0);
  removeProfile(profiles, 0);
  removeProfile(profiles, 0);

  Assert.equal(Array.from(service.profiles).length, 0, "All profiles gone.");
  Assert.equal(service.profileCount, 0, "All profiles gone.");
});