summaryrefslogtreecommitdiffstats
path: root/comm/calendar/test/unit/test_storage_connection.js
blob: 2984fa2dc40899febbffcddf025d6e04a6a22d8c (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
/* 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/. */

add_setup(async function () {
  do_get_profile();
  await new Promise(resolve => cal.manager.startup({ onResult: resolve }));
});

/**
 * Tests that local storage calendars share a database connection.
 */
add_task(async function testLocal() {
  let localCalendarA = cal.manager.createCalendar(
    "storage",
    Services.io.newURI(`moz-storage-calendar://`)
  );
  localCalendarA.id = cal.getUUID();
  let dbA = localCalendarA.wrappedJSObject.mStorageDb.db;

  let localCalendarB = cal.manager.createCalendar(
    "storage",
    Services.io.newURI(`moz-storage-calendar://`)
  );
  localCalendarB.id = cal.getUUID();
  let dbB = localCalendarB.wrappedJSObject.mStorageDb.db;

  Assert.equal(
    dbA.databaseFile.path,
    PathUtils.join(PathUtils.profileDir, "calendar-data", "local.sqlite"),
    "local calendar A uses the right database file"
  );
  Assert.equal(
    dbB.databaseFile.path,
    PathUtils.join(PathUtils.profileDir, "calendar-data", "local.sqlite"),
    "local calendar B uses the right database file"
  );
  Assert.equal(dbA, dbB, "local calendars share a database connection");
});

/**
 * Tests that local storage calendars using the same specified database file share a connection,
 * and that local storage calendars with a different specified database file do not.
 */
add_task(async function testLocalFile() {
  let testFileA = new FileUtils.File(PathUtils.join(PathUtils.tempDir, "file-a.sqlite"));
  let testFileB = new FileUtils.File(PathUtils.join(PathUtils.tempDir, "file-b.sqlite"));

  let fileCalendarA = cal.manager.createCalendar("storage", Services.io.newFileURI(testFileA));
  fileCalendarA.id = cal.getUUID();
  let dbA = fileCalendarA.wrappedJSObject.mStorageDb.db;

  let fileCalendarB = cal.manager.createCalendar("storage", Services.io.newFileURI(testFileB));
  fileCalendarB.id = cal.getUUID();
  let dbB = fileCalendarB.wrappedJSObject.mStorageDb.db;

  let fileCalendarC = cal.manager.createCalendar("storage", Services.io.newFileURI(testFileA));
  fileCalendarC.id = cal.getUUID();
  let dbC = fileCalendarC.wrappedJSObject.mStorageDb.db;

  Assert.equal(
    dbA.databaseFile.path,
    testFileA.path,
    "local calendar A uses the right database file"
  );
  Assert.equal(
    dbB.databaseFile.path,
    testFileB.path,
    "local calendar B uses the right database file"
  );
  Assert.equal(
    dbC.databaseFile.path,
    testFileA.path,
    "local calendar C uses the right database file"
  );
  Assert.notEqual(
    dbA,
    dbB,
    "calendars with different file URLs do not share a database connection"
  );
  Assert.notEqual(
    dbB,
    dbC,
    "calendars with different file URLs do not share a database connection"
  );
  Assert.equal(dbA, dbC, "calendars with matching file URLs share a database connection");
});

/**
 * Tests that cached network calendars share a database connection.
 */
add_task(async function testNetwork() {
  // Pretend to be offline so connecting to calendars that don't exist doesn't throw errors.
  Services.io.offline = true;

  let networkCalendarA = cal.manager.createCalendar(
    "ics",
    Services.io.newURI("http://localhost/ics")
  );
  networkCalendarA.id = cal.getUUID();
  networkCalendarA.setProperty("cache.enabled", true);
  cal.manager.registerCalendar(networkCalendarA);
  networkCalendarA = cal.manager.getCalendarById(networkCalendarA.id);
  let dbA = networkCalendarA.wrappedJSObject.mCachedCalendar.wrappedJSObject.mStorageDb.db;

  let networkCalendarB = cal.manager.createCalendar(
    "caldav",
    Services.io.newURI("http://localhost/caldav")
  );
  networkCalendarB.id = cal.getUUID();
  networkCalendarB.setProperty("cache.enabled", true);
  cal.manager.registerCalendar(networkCalendarB);
  networkCalendarB = cal.manager.getCalendarById(networkCalendarB.id);
  let dbB = networkCalendarB.wrappedJSObject.mCachedCalendar.wrappedJSObject.mStorageDb.db;

  Assert.equal(
    dbA.databaseFile.path,
    PathUtils.join(PathUtils.profileDir, "calendar-data", "cache.sqlite"),
    "network calendar A uses the right database file"
  );
  Assert.equal(
    dbB.databaseFile.path,
    PathUtils.join(PathUtils.profileDir, "calendar-data", "cache.sqlite"),
    "network calendar B uses the right database file"
  );
  Assert.equal(dbA, dbB, "network calendars share a database connection");
});