summaryrefslogtreecommitdiff
path: root/toolkit/components/places/tests/expiration/test_analyze_runs.js
blob: 1a84e1b38c783ffb6edeb4bf1742b4811067164e (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Constants

const TOPIC_AUTOCOMPLETE_FEEDBACK_INCOMING = "autocomplete-will-enter-text";

// Helpers

/**
 * Ensures that we have no data in the tables created by ANALYZE.
 */
function clearAnalyzeData() {
  let db = DBConn();
  if (!db.tableExists("sqlite_stat1")) {
    return;
  }
  db.executeSimpleSQL("DELETE FROM sqlite_stat1");
}

/**
 * Checks that we ran ANALYZE on the specified table.
 *
 * @param aTableName
 *        The table to check if ANALYZE was ran.
 * @param aRan
 *        True if it was expected to run, false otherwise
 */
function do_check_analyze_ran(aTableName, aRan) {
  let db = DBConn();
  do_check_true(db.tableExists("sqlite_stat1"));
  let stmt = db.createStatement("SELECT idx FROM sqlite_stat1 WHERE tbl = :table");
  stmt.params.table = aTableName;
  try {
    if (aRan) {
      do_check_true(stmt.executeStep());
      do_check_neq(stmt.row.idx, null);
    }
    else {
      do_check_false(stmt.executeStep());
    }
  }
  finally {
    stmt.finalize();
  }
}

// Tests

function run_test() {
  run_next_test();
}

add_task(function* init_tests() {
  const TEST_URI = NetUtil.newURI("http://mozilla.org/");
  const TEST_TITLE = "This is a test";

  yield PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    title: TEST_TITLE,
    url: TEST_URI
  });
  yield PlacesTestUtils.addVisits(TEST_URI);
  let thing = {
    QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteInput,
                                           Ci.nsIAutoCompletePopup,
                                           Ci.nsIAutoCompleteController]),
    get popup() { return thing; },
    get controller() { return thing; },
    popupOpen: true,
    selectedIndex: 0,
    getValueAt: function() { return TEST_URI.spec; },
    searchString: TEST_TITLE,
  };
  Services.obs.notifyObservers(thing, TOPIC_AUTOCOMPLETE_FEEDBACK_INCOMING,
                               null);
});

add_task(function* test_timed() {
  clearAnalyzeData();

  // Set a low interval and wait for the timed expiration to start.
  let promise = promiseTopicObserved(PlacesUtils.TOPIC_EXPIRATION_FINISHED);
  setInterval(3);
  yield promise;
  setInterval(3600);

  do_check_analyze_ran("moz_places", false);
  do_check_analyze_ran("moz_bookmarks", false);
  do_check_analyze_ran("moz_historyvisits", false);
  do_check_analyze_ran("moz_inputhistory", true);
});

add_task(function* test_debug() {
  clearAnalyzeData();

  yield promiseForceExpirationStep(1);

  do_check_analyze_ran("moz_places", true);
  do_check_analyze_ran("moz_bookmarks", true);
  do_check_analyze_ran("moz_historyvisits", true);
  do_check_analyze_ran("moz_inputhistory", true);
});

add_task(function* test_clear_history() {
  clearAnalyzeData();

  let promise = promiseTopicObserved(PlacesUtils.TOPIC_EXPIRATION_FINISHED);
  let listener = Cc["@mozilla.org/places/expiration;1"]
                 .getService(Ci.nsINavHistoryObserver);
  listener.onClearHistory();
  yield promise;

  do_check_analyze_ran("moz_places", true);
  do_check_analyze_ran("moz_bookmarks", false);
  do_check_analyze_ran("moz_historyvisits", true);
  do_check_analyze_ran("moz_inputhistory", true);
});