Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | /** |
michael@0 | 5 | * These tests check the auto-update facility of the thumbnail service. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | function runTests() { |
michael@0 | 9 | // A "trampoline" - a generator that iterates over sub-iterators |
michael@0 | 10 | let tests = [ |
michael@0 | 11 | simpleCaptureTest, |
michael@0 | 12 | capIfStaleErrorResponseUpdateTest, |
michael@0 | 13 | capIfStaleGoodResponseUpdateTest, |
michael@0 | 14 | regularCapErrorResponseUpdateTest, |
michael@0 | 15 | regularCapGoodResponseUpdateTest |
michael@0 | 16 | ]; |
michael@0 | 17 | for (let test of tests) { |
michael@0 | 18 | info("Running subtest " + test.name); |
michael@0 | 19 | for (let iterator of test()) |
michael@0 | 20 | yield iterator; |
michael@0 | 21 | } |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | function ensureThumbnailStale(url) { |
michael@0 | 25 | // We go behind the back of the thumbnail service and change the |
michael@0 | 26 | // mtime of the file to be in the past. |
michael@0 | 27 | let fname = PageThumbsStorage.getFilePathForURL(url); |
michael@0 | 28 | let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); |
michael@0 | 29 | file.initWithPath(fname); |
michael@0 | 30 | ok(file.exists(), fname + " should exist"); |
michael@0 | 31 | // Set it as very stale... |
michael@0 | 32 | file.lastModifiedTime = Date.now() - 1000000000; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | function getThumbnailModifiedTime(url) { |
michael@0 | 36 | let fname = PageThumbsStorage.getFilePathForURL(url); |
michael@0 | 37 | let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); |
michael@0 | 38 | file.initWithPath(fname); |
michael@0 | 39 | return file.lastModifiedTime; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | // The tests! |
michael@0 | 43 | /* Check functionality of a normal captureAndStoreIfStale request */ |
michael@0 | 44 | function simpleCaptureTest() { |
michael@0 | 45 | let numNotifications = 0; |
michael@0 | 46 | const URL = "http://mochi.test:8888/browser/toolkit/components/thumbnails/test/thumbnails_update.sjs?simple"; |
michael@0 | 47 | |
michael@0 | 48 | function observe(subject, topic, data) { |
michael@0 | 49 | is(topic, "page-thumbnail:create", "got expected topic"); |
michael@0 | 50 | is(data, URL, "data is our test URL"); |
michael@0 | 51 | if (++numNotifications == 2) { |
michael@0 | 52 | // This is the final notification and signals test success... |
michael@0 | 53 | Services.obs.removeObserver(observe, "page-thumbnail:create"); |
michael@0 | 54 | gBrowser.removeTab(gBrowser.selectedTab); |
michael@0 | 55 | next(); |
michael@0 | 56 | } |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | Services.obs.addObserver(observe, "page-thumbnail:create", false); |
michael@0 | 60 | // Create a tab - we don't care what the content is. |
michael@0 | 61 | yield addTab(URL); |
michael@0 | 62 | let browser = gBrowser.selectedBrowser; |
michael@0 | 63 | |
michael@0 | 64 | // Capture the screenshot. |
michael@0 | 65 | PageThumbs.captureAndStore(browser, function () { |
michael@0 | 66 | // We've got a capture so should have seen the observer. |
michael@0 | 67 | is(numNotifications, 1, "got notification of item being created."); |
michael@0 | 68 | // The capture is now "fresh" - so requesting the URL should not cause |
michael@0 | 69 | // a new capture. |
michael@0 | 70 | PageThumbs.captureAndStoreIfStale(browser); |
michael@0 | 71 | is(numNotifications, 1, "still only 1 notification of item being created."); |
michael@0 | 72 | |
michael@0 | 73 | ensureThumbnailStale(URL); |
michael@0 | 74 | // Ask for it to be updated. |
michael@0 | 75 | PageThumbs.captureAndStoreIfStale(browser); |
michael@0 | 76 | // But it's async, so wait - our observer above will call next() when |
michael@0 | 77 | // the notification comes. |
michael@0 | 78 | }); |
michael@0 | 79 | yield undefined // wait for callbacks to call 'next'... |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | /* Check functionality of captureAndStoreIfStale when there is an error response |
michael@0 | 83 | from the server. |
michael@0 | 84 | */ |
michael@0 | 85 | function capIfStaleErrorResponseUpdateTest() { |
michael@0 | 86 | const URL = "http://mochi.test:8888/browser/toolkit/components/thumbnails/test/thumbnails_update.sjs?fail"; |
michael@0 | 87 | yield addTab(URL); |
michael@0 | 88 | |
michael@0 | 89 | yield captureAndCheckColor(0, 255, 0, "we have a green thumbnail"); |
michael@0 | 90 | // update the thumbnail to be stale, then re-request it. The server will |
michael@0 | 91 | // return a 400 response and a red thumbnail. |
michael@0 | 92 | // The service should not save the thumbnail - so we (a) check the thumbnail |
michael@0 | 93 | // remains green and (b) check the mtime of the file is < now. |
michael@0 | 94 | ensureThumbnailStale(URL); |
michael@0 | 95 | yield navigateTo(URL); |
michael@0 | 96 | // now() returns a higher-precision value than the modified time of a file. |
michael@0 | 97 | // As we set the thumbnail very stale, allowing 1 second of "slop" here |
michael@0 | 98 | // works around this while still keeping the test valid. |
michael@0 | 99 | let now = Date.now() - 1000 ; |
michael@0 | 100 | PageThumbs.captureAndStoreIfStale(gBrowser.selectedBrowser, () => { |
michael@0 | 101 | ok(getThumbnailModifiedTime(URL) < now, "modified time should be < now"); |
michael@0 | 102 | retrieveImageDataForURL(URL, function ([r, g, b]) { |
michael@0 | 103 | is("" + [r,g,b], "" + [0, 255, 0], "thumbnail is still green"); |
michael@0 | 104 | gBrowser.removeTab(gBrowser.selectedTab); |
michael@0 | 105 | next(); |
michael@0 | 106 | }); |
michael@0 | 107 | }); |
michael@0 | 108 | yield undefined; // wait for callback to call 'next'... |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | /* Check functionality of captureAndStoreIfStale when there is a non-error |
michael@0 | 112 | response from the server. This test is somewhat redundant - although it is |
michael@0 | 113 | using a http:// URL instead of a data: url like most others. |
michael@0 | 114 | */ |
michael@0 | 115 | function capIfStaleGoodResponseUpdateTest() { |
michael@0 | 116 | const URL = "http://mochi.test:8888/browser/toolkit/components/thumbnails/test/thumbnails_update.sjs?ok"; |
michael@0 | 117 | yield addTab(URL); |
michael@0 | 118 | let browser = gBrowser.selectedBrowser; |
michael@0 | 119 | |
michael@0 | 120 | yield captureAndCheckColor(0, 255, 0, "we have a green thumbnail"); |
michael@0 | 121 | // update the thumbnail to be stale, then re-request it. The server will |
michael@0 | 122 | // return a 200 response and a red thumbnail - so that new thumbnail should |
michael@0 | 123 | // end up captured. |
michael@0 | 124 | ensureThumbnailStale(URL); |
michael@0 | 125 | yield navigateTo(URL); |
michael@0 | 126 | // now() returns a higher-precision value than the modified time of a file. |
michael@0 | 127 | // As we set the thumbnail very stale, allowing 1 second of "slop" here |
michael@0 | 128 | // works around this while still keeping the test valid. |
michael@0 | 129 | let now = Date.now() - 1000 ; |
michael@0 | 130 | PageThumbs.captureAndStoreIfStale(browser, () => { |
michael@0 | 131 | ok(getThumbnailModifiedTime(URL) >= now, "modified time should be >= now"); |
michael@0 | 132 | // the captureAndStoreIfStale request saw a 200 response with the red body, |
michael@0 | 133 | // so we expect to see the red version here. |
michael@0 | 134 | retrieveImageDataForURL(URL, function ([r, g, b]) { |
michael@0 | 135 | is("" + [r,g,b], "" + [255, 0, 0], "thumbnail is now red"); |
michael@0 | 136 | next(); |
michael@0 | 137 | }); |
michael@0 | 138 | }); |
michael@0 | 139 | yield undefined; // wait for callback to call 'next'... |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | /* Check functionality of captureAndStore when there is an error response |
michael@0 | 143 | from the server. |
michael@0 | 144 | */ |
michael@0 | 145 | function regularCapErrorResponseUpdateTest() { |
michael@0 | 146 | const URL = "http://mochi.test:8888/browser/toolkit/components/thumbnails/test/thumbnails_update.sjs?fail"; |
michael@0 | 147 | yield addTab(URL); |
michael@0 | 148 | yield captureAndCheckColor(0, 255, 0, "we have a green thumbnail"); |
michael@0 | 149 | gBrowser.removeTab(gBrowser.selectedTab); |
michael@0 | 150 | // do it again - the server will return a 400, so the foreground service |
michael@0 | 151 | // should not update it. |
michael@0 | 152 | yield addTab(URL); |
michael@0 | 153 | yield captureAndCheckColor(0, 255, 0, "we still have a green thumbnail"); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | /* Check functionality of captureAndStore when there is an OK response |
michael@0 | 157 | from the server. |
michael@0 | 158 | */ |
michael@0 | 159 | function regularCapGoodResponseUpdateTest() { |
michael@0 | 160 | const URL = "http://mochi.test:8888/browser/toolkit/components/thumbnails/test/thumbnails_update.sjs?ok"; |
michael@0 | 161 | yield addTab(URL); |
michael@0 | 162 | yield captureAndCheckColor(0, 255, 0, "we have a green thumbnail"); |
michael@0 | 163 | gBrowser.removeTab(gBrowser.selectedTab); |
michael@0 | 164 | // do it again - the server will return a 200, so the foreground service |
michael@0 | 165 | // should update it. |
michael@0 | 166 | yield addTab(URL); |
michael@0 | 167 | yield captureAndCheckColor(255, 0, 0, "we now have a red thumbnail"); |
michael@0 | 168 | } |