|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ |
|
3 */ |
|
4 |
|
5 // Tests the update part of the post-app-update dialog |
|
6 |
|
7 var gProvider; |
|
8 var gWin; |
|
9 |
|
10 function waitForView(aView, aCallback) { |
|
11 var view = gWin.document.getElementById(aView); |
|
12 if (view.parentNode.selectedPanel == view) { |
|
13 aCallback(); |
|
14 return; |
|
15 } |
|
16 |
|
17 view.addEventListener("ViewChanged", function() { |
|
18 view.removeEventListener("ViewChanged", arguments.callee, false); |
|
19 aCallback(); |
|
20 }, false); |
|
21 } |
|
22 |
|
23 function waitForClose(aCallback) { |
|
24 gWin.addEventListener("unload", function() { |
|
25 gWin.removeEventListener("unload", arguments.callee, false); |
|
26 |
|
27 aCallback(); |
|
28 }, false); |
|
29 } |
|
30 |
|
31 /** |
|
32 * Creates 4 test add-ons. Two are disabled and two enabled. |
|
33 */ |
|
34 function setupUI(aFailDownloads, aFailInstalls, aCallback) { |
|
35 if (gProvider) |
|
36 gProvider.unregister(); |
|
37 |
|
38 gProvider = new MockProvider(); |
|
39 |
|
40 for (var i = 1; i < 5; i++) { |
|
41 var addon = new MockAddon("test" + i + "@tests.mozilla.org", |
|
42 "Test Add-on " + i, "extension"); |
|
43 addon.version = "1.0"; |
|
44 addon.userDisabled = (i > 2); |
|
45 addon.appDisabled = false; |
|
46 addon.isActive = !addon.userDisabled && !addon.appDisabled; |
|
47 |
|
48 addon.findUpdates = function(aListener, aReason, aAppVersion, aPlatformVersion) { |
|
49 var newAddon = new MockAddon(this.id, this.name, "extension"); |
|
50 newAddon.version = "2.0"; |
|
51 var install = new MockInstall(this.name, this.type, newAddon); |
|
52 install.existingAddon = this; |
|
53 |
|
54 install.install = function() { |
|
55 this.state = AddonManager.STATE_DOWNLOADING; |
|
56 this.callListeners("onDownloadStarted"); |
|
57 |
|
58 var self = this; |
|
59 executeSoon(function() { |
|
60 if (aFailDownloads) { |
|
61 self.state = AddonManager.STATE_DOWNLOAD_FAILED; |
|
62 self.callListeners("onDownloadFailed"); |
|
63 return; |
|
64 } |
|
65 |
|
66 self.type = self._type; |
|
67 self.addon = new MockAddon(self.existingAddon.id, self.name, self.type); |
|
68 self.addon.version = self.version; |
|
69 self.addon.pendingOperations = AddonManager.PENDING_INSTALL; |
|
70 self.addon.install = self; |
|
71 |
|
72 self.existingAddon.pendingUpgrade = self.addon; |
|
73 self.existingAddon.pendingOperations |= AddonManager.PENDING_UPGRADE; |
|
74 |
|
75 self.state = AddonManager.STATE_DOWNLOADED; |
|
76 self.callListeners("onDownloadEnded"); |
|
77 |
|
78 self.state = AddonManager.STATE_INSTALLING; |
|
79 self.callListeners("onInstallStarted"); |
|
80 |
|
81 if (aFailInstalls) { |
|
82 self.state = AddonManager.STATE_INSTALL_FAILED; |
|
83 self.callListeners("onInstallFailed"); |
|
84 return; |
|
85 } |
|
86 |
|
87 self.state = AddonManager.STATE_INSTALLED; |
|
88 self.callListeners("onInstallEnded"); |
|
89 }); |
|
90 } |
|
91 |
|
92 aListener.onUpdateAvailable(this, install); |
|
93 |
|
94 aListener.onUpdateFinished(this, AddonManager.UPDATE_STATUS_NO_ERROR); |
|
95 }; |
|
96 |
|
97 gProvider.addAddon(addon); |
|
98 } |
|
99 |
|
100 gWin = Services.ww.openWindow(null, |
|
101 "chrome://mozapps/content/extensions/selectAddons.xul", |
|
102 "", |
|
103 "chrome,centerscreen,dialog,titlebar", |
|
104 null); |
|
105 waitForFocus(function() { |
|
106 waitForView("select", function() { |
|
107 var row = gWin.document.getElementById("select-rows").firstChild.nextSibling; |
|
108 while (row) { |
|
109 if (!row.id || row.id.indexOf("@tests.mozilla.org") < 0) { |
|
110 // not a test add-on |
|
111 row = row.nextSibling; |
|
112 continue; |
|
113 } |
|
114 |
|
115 if (row.id == "test2@tests.mozilla.org" || |
|
116 row.id == "test4@tests.mozilla.org") { |
|
117 row.disable(); |
|
118 } |
|
119 else { |
|
120 row.keep(); |
|
121 } |
|
122 row = row.nextSibling; |
|
123 } |
|
124 |
|
125 waitForView("confirm", function() { |
|
126 waitForView("update", aCallback); |
|
127 EventUtils.synthesizeMouseAtCenter(gWin.document.getElementById("next"), {}, gWin); |
|
128 }); |
|
129 EventUtils.synthesizeMouseAtCenter(gWin.document.getElementById("next"), {}, gWin); |
|
130 }); |
|
131 }, gWin); |
|
132 } |
|
133 |
|
134 function test() { |
|
135 waitForExplicitFinish(); |
|
136 run_next_test(); |
|
137 } |
|
138 |
|
139 function end_test() { |
|
140 finish(); |
|
141 } |
|
142 |
|
143 // Test for working updates |
|
144 add_test(function working_test() { |
|
145 setupUI(false, false, function() { |
|
146 waitForClose(function() { |
|
147 is(gWin.document.getElementById("update-progress").value, 2, "Should have finished 2 downloads"); |
|
148 run_next_test(); |
|
149 }); |
|
150 |
|
151 EventUtils.synthesizeMouseAtCenter(gWin.document.getElementById("next"), {}, gWin); |
|
152 }); |
|
153 }); |
|
154 |
|
155 // Test for failed updates |
|
156 add_test(function working_test() { |
|
157 setupUI(true, false, function() { |
|
158 waitForView("errors", function() { |
|
159 is(gWin.document.getElementById("update-progress").value, 2, "Should have finished 2 downloads"); |
|
160 gWin.close(); |
|
161 |
|
162 run_next_test(); |
|
163 }); |
|
164 |
|
165 EventUtils.synthesizeMouseAtCenter(gWin.document.getElementById("next"), {}, gWin); |
|
166 }); |
|
167 }); |
|
168 |
|
169 // Test for failed updates |
|
170 add_test(function working_test() { |
|
171 setupUI(false, true, function() { |
|
172 waitForView("errors", function() { |
|
173 is(gWin.document.getElementById("update-progress").value, 2, "Should have finished 2 downloads"); |
|
174 gWin.close(); |
|
175 |
|
176 run_next_test(); |
|
177 }); |
|
178 |
|
179 EventUtils.synthesizeMouseAtCenter(gWin.document.getElementById("next"), {}, gWin); |
|
180 }); |
|
181 }); |