summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/test/resources/searchTestUtils.js
blob: df26824bcac9cf0beedc6d3fea1c9f7708a82662 (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
/* 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/. */

// Contains various functions commonly used in testing mailnews search.

/**
 * TestSearch: Class to test number of search hits
 *
 * @param {nsIMsgFolder} aFolder - The folder to search
 * @param {string|integer} aValue - value used for the search
 *                   The interpretation of aValue depends on aAttrib. It
 *                   defaults to string, but for certain attributes other
 *                   types are used.
 *                   WARNING: not all attributes have been tested.
 *
 * @param {nsMsgSearchAttrib} aAttrib - Attribute for the search (Ci.nsMsgSearchAttrib.Size, etc.)
 * @param {nsMsgSearchOp} aOp - Operation for the search (Ci.nsMsgSearchOp.Contains, etc.)
 * @param {integer} aHitCount - Expected number of search hits
 * @param {Function} onDone - Function to call on completion of search
 * @param {string} aCustomId - Id string for the custom action, if aAttrib is Custom
 * @param {string} aArbitraryHeader - For OtherHeader case, header.
 * @param {string|integer} aHdrProperty - For HdrProperty and Uint32HdrProperty case
 *
 */
function TestSearch(
  aFolder,
  aValue,
  aAttrib,
  aOp,
  aHitCount,
  onDone,
  aCustomId,
  aArbitraryHeader,
  aHdrProperty
) {
  var searchListener = {
    onSearchHit(dbHdr, folder) {
      hitCount++;
    },
    onSearchDone(status) {
      print("Finished search does " + aHitCount + " equal " + hitCount + "?");
      searchSession = null;
      Assert.equal(aHitCount, hitCount);
      if (onDone) {
        onDone();
      }
    },
    onNewSearch() {
      hitCount = 0;
    },
  };

  // define and initiate the search session

  var hitCount;
  var searchSession = Cc[
    "@mozilla.org/messenger/searchSession;1"
  ].createInstance(Ci.nsIMsgSearchSession);
  searchSession.addScopeTerm(Ci.nsMsgSearchScope.offlineMail, aFolder);
  var searchTerm = searchSession.createTerm();
  searchTerm.attrib = aAttrib;

  var value = searchTerm.value;
  // This is tricky - value.attrib must be set before actual values
  value.attrib = aAttrib;
  if (aAttrib == Ci.nsMsgSearchAttrib.JunkPercent) {
    value.junkPercent = aValue;
  } else if (aAttrib == Ci.nsMsgSearchAttrib.Priority) {
    value.priority = aValue;
  } else if (aAttrib == Ci.nsMsgSearchAttrib.Date) {
    value.date = aValue;
  } else if (
    aAttrib == Ci.nsMsgSearchAttrib.MsgStatus ||
    aAttrib == Ci.nsMsgSearchAttrib.FolderFlag ||
    aAttrib == Ci.nsMsgSearchAttrib.Uint32HdrProperty
  ) {
    value.status = aValue;
  } else if (aAttrib == Ci.nsMsgSearchAttrib.MessageKey) {
    value.msgKey = aValue;
  } else if (aAttrib == Ci.nsMsgSearchAttrib.Size) {
    value.size = aValue;
  } else if (aAttrib == Ci.nsMsgSearchAttrib.AgeInDays) {
    value.age = aValue;
  } else if (aAttrib == Ci.nsMsgSearchAttrib.JunkStatus) {
    value.junkStatus = aValue;
  } else if (aAttrib == Ci.nsMsgSearchAttrib.HasAttachmentStatus) {
    value.status = Ci.nsMsgMessageFlags.Attachment;
  } else {
    value.str = aValue;
  }
  searchTerm.value = value;
  searchTerm.op = aOp;
  searchTerm.booleanAnd = false;
  if (aAttrib == Ci.nsMsgSearchAttrib.Custom) {
    searchTerm.customId = aCustomId;
  } else if (aAttrib == Ci.nsMsgSearchAttrib.OtherHeader) {
    searchTerm.arbitraryHeader = aArbitraryHeader;
  } else if (
    aAttrib == Ci.nsMsgSearchAttrib.HdrProperty ||
    aAttrib == Ci.nsMsgSearchAttrib.Uint32HdrProperty
  ) {
    searchTerm.hdrProperty = aHdrProperty;
  }

  searchSession.appendTerm(searchTerm);
  searchSession.registerListener(searchListener);
  searchSession.search(null);
}

/*
 * Test search validity table Available and Enabled settings
 *
 * @param aScope:  search scope (Ci.nsMsgSearchScope.offlineMail, etc.)
 * @param aOp:     search operation (Ci.nsMsgSearchOp.Contains, etc.)
 * @param aAttrib: search attribute (Ci.nsMsgSearchAttrib.Size, etc.)
 * @param aValue:  expected value (true/false) for Available and Enabled
 */
const gValidityManager = Cc[
  "@mozilla.org/mail/search/validityManager;1"
].getService(Ci.nsIMsgSearchValidityManager);

function testValidityTable(aScope, aOp, aAttrib, aValue) {
  var validityTable = gValidityManager.getTable(aScope);
  var isAvailable = validityTable.getAvailable(aAttrib, aOp);
  var isEnabled = validityTable.getEnabled(aAttrib, aOp);
  if (aValue) {
    Assert.ok(isAvailable);
    Assert.ok(isEnabled);
  } else {
    Assert.ok(!isAvailable);
    Assert.ok(!isEnabled);
  }
}