summaryrefslogtreecommitdiff
path: root/toolkit/modules/tests/browser/browser_PageMetadata.js
blob: ca6e18368755b845fbe6cfa8de34c913364d9270 (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
/**
 * Tests PageMetadata.jsm, which extracts metadata and microdata from a
 * document.
 */

var {PageMetadata} = Cu.import("resource://gre/modules/PageMetadata.jsm", {});

var rootURL = "http://example.com/browser/toolkit/modules/tests/browser/";

function promiseDocument(fileName) {
  let url = rootURL + fileName;

  return new Promise((resolve, reject) => {
    let xhr = new XMLHttpRequest();
    xhr.onload = () => resolve(xhr.responseXML);
    xhr.onerror = () => reject(new Error("Error loading document"));
    xhr.open("GET", url);
    xhr.responseType = "document";
    xhr.send();
  });
}

/**
 * Load a simple document.
 */
add_task(function* simpleDoc() {
  let fileName = "metadata_simple.html";
  info(`Loading a simple page, ${fileName}`);

  let doc = yield promiseDocument(fileName);
  Assert.notEqual(doc, null,
                  "Should have a document to analyse");

  let data = PageMetadata.getData(doc);
  Assert.notEqual(data, null,
                  "Should have non-null result");
  Assert.equal(data.url, rootURL + fileName,
               "Should have expected url property");
  Assert.equal(data.title, "Test Title",
               "Should have expected title property");
  Assert.equal(data.description, "A very simple test page",
               "Should have expected title property");
});

add_task(function* titlesDoc() {
  let fileName = "metadata_titles.html";
  info(`Loading titles page, ${fileName}`);

  let doc = yield promiseDocument(fileName);
  Assert.notEqual(doc, null,
                  "Should have a document to analyse");

  let data = PageMetadata.getData(doc);
  Assert.notEqual(data, null,
                  "Should have non-null result");
  Assert.equal(data.title, "Test Titles",
               "Should use the page title, not the open graph title");
});

add_task(function* titlesFallbackDoc() {
  let fileName = "metadata_titles_fallback.html";
  info(`Loading titles page, ${fileName}`);

  let doc = yield promiseDocument(fileName);
  Assert.notEqual(doc, null,
                  "Should have a document to analyse");

  let data = PageMetadata.getData(doc);
  Assert.notEqual(data, null,
                  "Should have non-null result");
  Assert.equal(data.title, "Title",
               "Should use the open graph title");
});