summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/webextensions/test/xpcshell/test_bug620837.js
blob: 6bfbfcaf2c4573c34678d424e1fc9a4f10bb15de (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
138
139
140
141
142
143
144
145
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

Components.utils.import("resource://testing-common/httpd.js");

const PREF_BLOCKLIST_LASTUPDATETIME   = "app.update.lastUpdateTime.blocklist-background-update-timer";
const PREF_BLOCKLIST_PINGCOUNTTOTAL   = "extensions.blocklist.pingCountTotal";
const PREF_BLOCKLIST_PINGCOUNTVERSION = "extensions.blocklist.pingCountVersion";

const SECONDS_IN_DAY = 60 * 60 * 24;

var gExpectedQueryString = null;
var gNextTest = null;
var gTestserver = null;

function notify_blocklist() {
  var blocklist = AM_Cc["@mozilla.org/extensions/blocklist;1"].
                  getService(AM_Ci.nsITimerCallback);
  blocklist.notify(null);
}

function pathHandler(metadata, response) {
  do_check_eq(metadata.queryString, gExpectedQueryString);
  gNextTest();
}

function run_test() {
  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1");

  gTestserver = new HttpServer();
  gTestserver.registerPathHandler("/", pathHandler);
  gTestserver.start(-1);
  gPort = gTestserver.identity.primaryPort;

  Services.prefs.setCharPref("extensions.blocklist.url",
                             "http://localhost:" + gPort +
                             "/?%PING_COUNT%&%TOTAL_PING_COUNT%&%DAYS_SINCE_LAST_PING%");

  do_test_pending();
  test1();
}

function getNowInSeconds() {
  return Math.round(Date.now() / 1000);
}

function test1() {
  gNextTest = test2;
  gExpectedQueryString = "1&1&new";
  notify_blocklist();
}

function test2() {
  gNextTest = test3;
  gExpectedQueryString = "invalid&invalid&invalid";
  notify_blocklist();
}

function test3() {
  Services.prefs.setIntPref(PREF_BLOCKLIST_LASTUPDATETIME,
                            (getNowInSeconds() - SECONDS_IN_DAY));
  gNextTest = test4;
  gExpectedQueryString = "2&2&1";
  notify_blocklist();
}

function test4() {
  Services.prefs.setIntPref(PREF_BLOCKLIST_PINGCOUNTVERSION, -1);
  Services.prefs.setIntPref(PREF_BLOCKLIST_LASTUPDATETIME,
                            (getNowInSeconds() - (SECONDS_IN_DAY * 2)));
  gNextTest = test5;
  gExpectedQueryString = "1&3&2";
  notify_blocklist();
}

function test5() {
  Services.prefs.setIntPref(PREF_BLOCKLIST_LASTUPDATETIME, getNowInSeconds());
  gNextTest = test6;
  gExpectedQueryString = "invalid&invalid&0";
  notify_blocklist();
}

function test6() {
  Services.prefs.setIntPref(PREF_BLOCKLIST_LASTUPDATETIME,
                            (getNowInSeconds() - (SECONDS_IN_DAY * 3)));
  gNextTest = test7;
  gExpectedQueryString = "2&4&3";
  notify_blocklist();
}

function test7() {
  Services.prefs.setIntPref(PREF_BLOCKLIST_PINGCOUNTVERSION, 2147483647);
  Services.prefs.setIntPref(PREF_BLOCKLIST_LASTUPDATETIME,
                            (getNowInSeconds() - (SECONDS_IN_DAY * 4)));
  gNextTest = test8;
  gExpectedQueryString = "2147483647&5&4";
  notify_blocklist();
}

function test8() {
  Services.prefs.setIntPref(PREF_BLOCKLIST_LASTUPDATETIME,
                            (getNowInSeconds() - (SECONDS_IN_DAY * 5)));
  gNextTest = test9;
  gExpectedQueryString = "1&6&5";
  notify_blocklist();
}

function test9() {
  Services.prefs.setIntPref(PREF_BLOCKLIST_PINGCOUNTTOTAL, 2147483647);
  Services.prefs.setIntPref(PREF_BLOCKLIST_LASTUPDATETIME,
                            (getNowInSeconds() - (SECONDS_IN_DAY * 6)));
  gNextTest = test10;
  gExpectedQueryString = "2&2147483647&6";
  notify_blocklist();
}

function test10() {
  Services.prefs.setIntPref(PREF_BLOCKLIST_LASTUPDATETIME,
                            (getNowInSeconds() - (SECONDS_IN_DAY * 7)));
  gNextTest = test11;
  gExpectedQueryString = "3&1&7";
  notify_blocklist();
}

function test11() {
  Services.prefs.setIntPref(PREF_BLOCKLIST_PINGCOUNTVERSION, -1);
  Services.prefs.setIntPref(PREF_BLOCKLIST_LASTUPDATETIME,
                            (getNowInSeconds() - (SECONDS_IN_DAY * 8)));
  gNextTest = test12;
  gExpectedQueryString = "1&2&8";
  notify_blocklist();
}

function test12() {
  Services.prefs.setIntPref(PREF_BLOCKLIST_LASTUPDATETIME,
                            (getNowInSeconds() - (SECONDS_IN_DAY * 9)));
  gNextTest = finish;
  gExpectedQueryString = "2&3&9";
  notify_blocklist();
}

function finish() {
  gTestserver.stop(do_test_finished);
}