summaryrefslogtreecommitdiffstats
path: root/toolkit/components/pdfjs/test/browser_pdfjs_find.js
blob: 24a3a509675844fd00ed9eebaeeb378d1730966e (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
128
129
130
131
132
133
134
135
136
137
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TESTROOT = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content/",
  "http://mochi.test:8888/"
);

// Get a ref to the pdfs we want to open.
const OS_PDF_URL = TESTROOT + "file_pdfjs_object_stream.pdf";
const TEST_PDF_URL = TESTROOT + "file_pdfjs_test.pdf";

add_task(async function test_find_octet_stream_pdf() {
  await BrowserTestUtils.withNewTab(OS_PDF_URL, async browser => {
    let findEls = ["cmd_find", "cmd_findAgain", "cmd_findPrevious"].map(id =>
      document.getElementById(id)
    );
    for (let el of findEls) {
      ok(!el.hasAttribute("disabled"), `${el.id} should be enabled`);
    }
  });
});

// This code is roughly based on `promiseFindFinished` from
// `toolkit/content/tests/browser/head.js`.
function waitForFinderResult(findbar) {
  return new Promise(resolve => {
    let resultListener = {
      onFindResult(data) {
        findbar.browser.finder.removeResultListener(resultListener);
        resolve(data);
      },
      onCurrentSelection() {},
      onMatchesCountResult() {},
      onHighlightFinished() {},
    };
    findbar.browser.finder.addResultListener(resultListener);
  });
}

function waitForPdfjsResult(findbar) {
  // FIXME: This is a pretty sketchy way to intercept the results from pdfjs...
  return new Promise(resolve => {
    let oldUpdateControlState = findbar.updateControlState;
    function updateControlState(result, findPrevious) {
      if (result !== Ci.nsITypeAheadFind.FIND_PENDING) {
        resolve({ result, findPrevious });
        if (this.updateControlState === updateControlState) {
          this.updateControlState = oldUpdateControlState;
        }
      }
      return oldUpdateControlState.call(this, result, findPrevious);
    }
    findbar.updateControlState = updateControlState;
  });
}

// This code is roughly based on `promiseFindFinished` from
// `toolkit/content/tests/browser/head.js`.
async function doFind(findbar, searchText, waitFunc) {
  info(`performing a find for ${searchText}`);
  findbar.startFind(findbar.FIND_NORMAL);
  let highlightElement = findbar.getElement("highlight");
  if (highlightElement.checked) {
    highlightElement.click();
  }
  await new Promise(resolve => executeSoon(resolve));
  findbar._findField.value = searchText;
  let promise = waitFunc(findbar);
  findbar._find();
  return promise;
}

add_task(async function test_findbar_in_pdf() {
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_PDF_URL);
  let findbar = await gBrowser.getFindBar(tab);
  let findResult = await doFind(findbar, "Mozilla", waitForPdfjsResult);
  is(
    findResult.result,
    Ci.nsITypeAheadFind.FIND_FOUND,
    "The Mozilla string was found in the PDF document"
  );
  BrowserTestUtils.removeTab(tab);
});

add_task(async function test_findbar_in_pdf_after_adopt() {
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_PDF_URL);
  let newWindow = await BrowserTestUtils.openNewBrowserWindow();

  info("adopting tab into new window");
  let newTab = newWindow.gBrowser.adoptTab(tab);

  let findbar = await newWindow.gBrowser.getFindBar(newTab);
  let findResult = await doFind(findbar, "Mozilla", waitForPdfjsResult);
  is(
    findResult.result,
    Ci.nsITypeAheadFind.FIND_FOUND,
    "The Mozilla string was found in the PDF document"
  );
  await BrowserTestUtils.closeWindow(newWindow);
});

// Make sure that performing a find in the browser continues to work after
// navigating to another page (i.e. Pdfjs disables its pdfjs interception
// listeners).
add_task(async function test_findbar_after_navigate() {
  await BrowserTestUtils.withNewTab(TEST_PDF_URL, async browser => {
    let tab = gBrowser.getTabForBrowser(browser);

    ok(
      !gBrowser.isFindBarInitialized(tab),
      "Findbar shouldn't be initialized yet"
    );

    info("navigating to a webpage");
    BrowserTestUtils.startLoadingURIString(
      browser,
      "http://mochi.test:8888/document-builder.sjs?html=<h1>hello, world!</h1>"
    );
    await BrowserTestUtils.browserLoaded(browser);

    ok(
      !gBrowser.isFindBarInitialized(tab),
      "Findbar still shouldn't be initialized after navigation"
    );

    let findbar = await gBrowser.getFindBar(tab);
    let findResult = await doFind(findbar, "hello", waitForFinderResult);
    is(
      findResult.result,
      Ci.nsITypeAheadFind.FIND_FOUND,
      "The hello string was found in the HTML document"
    );
  });
});