summaryrefslogtreecommitdiffstats
path: root/toolkit/components/feeds/test/test_xml.js
blob: 5bc0d759d12aa487edecf5b640e052337dc9f429 (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
 * 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/. */

/* test_xml.js
 * This file sets up the unit test environment by building an array of files
 * to be tested. It assumes it lives in a folder adjacent to the a folder
 * called 'xml', where the testcases live.
 *
 * The directory layout looks something like this:
 *
 * tests/test_xml.js*
 *      |
 *      - head.js
 *      |
 *      - xml/ -- rss1/...
 *             |
 *             -- rss2/...
 *             |
 *             -- atom/testcase.xml
 *
 * To add more tests, just include the file in the xml subfolder and add its name to xpcshell.ini
 */

"use strict";

// Listens to feeds being loaded. Runs the tests built into the feed afterwards to veryify they
// were parsed correctly.
function FeedListener(testcase) {
  this.testcase = testcase;
}

FeedListener.prototype = {
  handleResult: function(result) {
    var feed = result.doc;
    try {
      do_print("Testing feed " + this.testcase.file.path);
      Assert.ok(isIID(feed, Ci.nsIFeed), "Has feed interface");

      if (!eval(this.testcase.expect)) {
        Assert.ok(false, "expect failed for " + this.testcase.desc);
      } else {
        Assert.ok(true, "expect passed for " + this.testcase.desc);
      }
    } catch (e) {
      Assert.ok(false, "expect failed for " + this.testcase.desc + " ---- " + e.message);
    }

    run_next_test();
  }
}

function createTest(data) {
  return function() {
    var uri;

    if (data.base == null) {
      uri = NetUtil.newURI('http://example.org/' + data.path);
    } else {
      uri = data.base;
    }

    do_print("Testing " + data.file.leafName);

    var parser = Cc["@mozilla.org/feed-processor;1"].createInstance(Ci.nsIFeedProcessor);
    var stream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
    stream.init(data.file, 0x01, parseInt("0444", 8), 0);
    parser.listener = new FeedListener(data);

    try {
      parser.parseFromStream(stream, uri);
    } catch (e) {
      Assert.ok(false, "parse failed for " + data.file.leafName + " ---- " + e.message);
      // If the parser failed, the listener won't be notified, run the next test here.
      run_next_test();
    } finally {
      stream.close();
    }
  }
}

function run_test() {
  // Get the 'xml' directory in here
  var topDir = Services.dirsvc.get("CurWorkD", Ci.nsIFile);
  topDir.append("xml");

  // Every file in the test dir contains an encapulated RSS "test". Iterate through
  // them all and add them to the test runner.
  iterateDir(topDir, true, file => {
    var data = readTestData(file);
    add_test(createTest(data));
  });

  // Now run!
  run_next_test();
}