summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_migrateAddonRepository.js
blob: ad8bd5bca085d7663b1799e0ffb218fd4fb822ca (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

const EXPECTED_SCHEMA_VERSION = 4;
let dbfile;

function run_test() {
  do_test_pending();
  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");

  // Write out a minimal database.
  dbfile = gProfD.clone();
  dbfile.append("addons.sqlite");
  let db = AM_Cc["@mozilla.org/storage/service;1"].
           getService(AM_Ci.mozIStorageService).
           openDatabase(dbfile);

  db.createTable("addon",
                 "internal_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                 "id TEXT UNIQUE, " +
                 "type TEXT, " +
                 "name TEXT, " +
                 "version TEXT, " +
                 "creator TEXT, " +
                 "creatorURL TEXT, " +
                 "description TEXT, " +
                 "fullDescription TEXT, " +
                 "developerComments TEXT, " +
                 "eula TEXT, " +
                 "iconURL TEXT, " +
                 "homepageURL TEXT, " +
                 "supportURL TEXT, " +
                 "contributionURL TEXT, " +
                 "contributionAmount TEXT, " +
                 "averageRating INTEGER, " +
                 "reviewCount INTEGER, " +
                 "reviewURL TEXT, " +
                 "totalDownloads INTEGER, " +
                 "weeklyDownloads INTEGER, " +
                 "dailyUsers INTEGER, " +
                 "sourceURI TEXT, " +
                 "repositoryStatus INTEGER, " +
                 "size INTEGER, " +
                 "updateDate INTEGER");

  db.createTable("developer",
                 "addon_internal_id INTEGER, " +
                 "num INTEGER, " +
                 "name TEXT, " +
                 "url TEXT, " +
                 "PRIMARY KEY (addon_internal_id, num)");

  db.createTable("screenshot",
                 "addon_internal_id INTEGER, " +
                 "num INTEGER, " +
                 "url TEXT, " +
                 "thumbnailURL TEXT, " +
                 "caption TEXT, " +
                 "PRIMARY KEY (addon_internal_id, num)");

  let stmt = db.createStatement("INSERT INTO addon (id) VALUES (:id)");
  stmt.params.id = "test1@tests.mozilla.org";
  stmt.execute();
  stmt.finalize();

  stmt = db.createStatement("INSERT INTO screenshot VALUES " +
                            "(:addon_internal_id, :num, :url, :thumbnailURL, :caption)");

  stmt.params.addon_internal_id = 1;
  stmt.params.num = 0;
  stmt.params.url = "http://localhost/full1-1.png";
  stmt.params.thumbnailURL = "http://localhost/thumbnail1-1.png";
  stmt.params.caption = "Caption 1 - 1";
  stmt.execute();
  stmt.finalize();

  db.schemaVersion = 1;
  db.close();


  Services.prefs.setBoolPref("extensions.getAddons.cache.enabled", true);
  AddonRepository.getCachedAddonByID("test1@tests.mozilla.org", function (aAddon) {
    do_check_neq(aAddon, null);
    do_check_eq(aAddon.screenshots.length, 1);
    do_check_true(aAddon.screenshots[0].width === null);
    do_check_true(aAddon.screenshots[0].height === null);
    do_check_true(aAddon.screenshots[0].thumbnailWidth === null);
    do_check_true(aAddon.screenshots[0].thumbnailHeight === null);
    do_check_eq(aAddon.iconURL, undefined);
    do_check_eq(JSON.stringify(aAddon.icons), "{}");
    AddonRepository.shutdown().then(
      function checkAfterRepoShutdown() {
        // Check the DB schema has changed once AddonRepository has freed it.
        db = AM_Cc["@mozilla.org/storage/service;1"].
             getService(AM_Ci.mozIStorageService).
             openDatabase(dbfile);
        do_check_eq(db.schemaVersion, EXPECTED_SCHEMA_VERSION);
        do_check_true(db.indexExists("developer_idx"));
        do_check_true(db.indexExists("screenshot_idx"));
        do_check_true(db.indexExists("compatibility_override_idx"));
        do_check_true(db.tableExists("compatibility_override"));
        do_check_true(db.indexExists("icon_idx"));
        do_check_true(db.tableExists("icon"));

        // Check the trigger is working
        db.executeSimpleSQL("INSERT INTO addon (id, type, name) VALUES('test_addon', 'extension', 'Test Addon')");
        let internalID = db.lastInsertRowID;
        db.executeSimpleSQL("INSERT INTO compatibility_override (addon_internal_id, num, type) VALUES('" + internalID + "', '1', 'incompatible')");

        let stmt = db.createStatement("SELECT COUNT(*) AS count FROM compatibility_override");
        stmt.executeStep();
        do_check_eq(stmt.row.count, 1);
        stmt.reset();

        db.executeSimpleSQL("DELETE FROM addon");
        stmt.executeStep();
        do_check_eq(stmt.row.count, 0);
        stmt.finalize();

        db.close();
        do_test_finished();
      },
      do_report_unexpected_exception
    );
  });
}