Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | const Profiler = Cc["@mozilla.org/tools/profiler;1"].getService(Ci.nsIProfiler); |
michael@0 | 7 | |
michael@0 | 8 | function run_test() |
michael@0 | 9 | { |
michael@0 | 10 | // Ensure the profiler is not running when the test starts (it could |
michael@0 | 11 | // happen if the MOZ_PROFILER_STARTUP environment variable is set) |
michael@0 | 12 | Profiler.StopProfiler(); |
michael@0 | 13 | DebuggerServer.init(function () { return true; }); |
michael@0 | 14 | DebuggerServer.addBrowserActors(); |
michael@0 | 15 | var client = new DebuggerClient(DebuggerServer.connectPipe()); |
michael@0 | 16 | client.connect(function () { |
michael@0 | 17 | client.listTabs(function(aResponse) { |
michael@0 | 18 | test_profiler_actor(client, aResponse.profilerActor); |
michael@0 | 19 | }); |
michael@0 | 20 | }); |
michael@0 | 21 | do_test_pending(); |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | function test_profiler_actor(aClient, aProfiler) |
michael@0 | 25 | { |
michael@0 | 26 | aClient.request({ to: aProfiler, type: "isActive" }, function (aResponse) { |
michael@0 | 27 | do_check_false(aResponse.isActive); |
michael@0 | 28 | |
michael@0 | 29 | aClient.request({ to: aProfiler, type: "getFeatures" }, function (aResponse) { |
michael@0 | 30 | var features = Profiler.GetFeatures([]); |
michael@0 | 31 | do_check_eq(aResponse.features.length, features.length); |
michael@0 | 32 | for (var i = 0; i < features.length; i++) |
michael@0 | 33 | do_check_eq(aResponse.features[i], features[i]); |
michael@0 | 34 | |
michael@0 | 35 | aClient.request({ to: aProfiler, type: "startProfiler", features: ['jank', 'js'] }, function (aResponse) { |
michael@0 | 36 | do_check_eq(typeof aResponse.msg, "string"); |
michael@0 | 37 | aClient.request({ to: aProfiler, type: "isActive" }, function (aResponse) { |
michael@0 | 38 | do_check_true(aResponse.isActive); |
michael@0 | 39 | |
michael@0 | 40 | aClient.request({ to: aProfiler, type: "getResponsivenessTimes" }, function (aResponse) { |
michael@0 | 41 | do_check_eq(typeof aResponse.responsivenessTimes, "object"); |
michael@0 | 42 | |
michael@0 | 43 | aClient.request({ to: aProfiler, type: "getSharedLibraryInformation" }, function (aResponse) { |
michael@0 | 44 | do_check_eq(typeof aResponse.sharedLibraryInformation, "string"); |
michael@0 | 45 | try { |
michael@0 | 46 | JSON.parse(aResponse.sharedLibraryInformation); |
michael@0 | 47 | } catch(e) { |
michael@0 | 48 | do_throw(e.toString(), Components.stack.caller); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | test_event_notifications(aClient, aProfiler); |
michael@0 | 52 | }); |
michael@0 | 53 | }); |
michael@0 | 54 | }); |
michael@0 | 55 | }); |
michael@0 | 56 | }); |
michael@0 | 57 | }); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | function test_event_notifications(aClient, aProfiler) |
michael@0 | 61 | { |
michael@0 | 62 | aClient.request({ to: aProfiler, type: "registerEventNotifications", events: ["foo", "bar"] }, function (aResponse) { |
michael@0 | 63 | do_check_eq(typeof aResponse.registered, "object"); |
michael@0 | 64 | do_check_eq(aResponse.registered.length, 2); |
michael@0 | 65 | do_check_eq(aResponse.registered[0], "foo"); |
michael@0 | 66 | do_check_eq(aResponse.registered[1], "bar"); |
michael@0 | 67 | |
michael@0 | 68 | aClient.request({ to: aProfiler, type: "registerEventNotifications", events: ["foo"] }, function (aResponse) { |
michael@0 | 69 | do_check_eq(typeof aResponse.registered, "object"); |
michael@0 | 70 | do_check_eq(aResponse.registered.length, 0); |
michael@0 | 71 | |
michael@0 | 72 | aClient.addListener("eventNotification", function (aType, aData) { |
michael@0 | 73 | do_check_eq(aType, "eventNotification"); |
michael@0 | 74 | do_check_eq(aData.event, "foo"); |
michael@0 | 75 | do_check_eq(typeof aData.subject, "object"); |
michael@0 | 76 | do_check_eq(aData.subject.foo, "foo"); |
michael@0 | 77 | do_check_eq(aData.data, "foo"); |
michael@0 | 78 | }); |
michael@0 | 79 | var subject = { foo: "foo" }; |
michael@0 | 80 | subject.wrappedJSObject = subject; |
michael@0 | 81 | Services.obs.notifyObservers(subject, "foo", "foo"); |
michael@0 | 82 | |
michael@0 | 83 | aClient.request({ to: aProfiler, type: "unregisterEventNotifications", events: ["foo", "bar", "qux"] }, function (aResponse) { |
michael@0 | 84 | do_check_eq(typeof aResponse.unregistered, "object"); |
michael@0 | 85 | do_check_eq(aResponse.unregistered.length, 2); |
michael@0 | 86 | do_check_eq(aResponse.unregistered[0], "foo"); |
michael@0 | 87 | do_check_eq(aResponse.unregistered[1], "bar"); |
michael@0 | 88 | |
michael@0 | 89 | // All events being now unregistered, sending an event shouldn't |
michael@0 | 90 | // do anything. If it does, the eventNotification listener above |
michael@0 | 91 | // will catch the event and fail on the aData.event test. |
michael@0 | 92 | Services.obs.notifyObservers(null, "bar", null); |
michael@0 | 93 | |
michael@0 | 94 | test_profile(aClient, aProfiler); |
michael@0 | 95 | }); |
michael@0 | 96 | }); |
michael@0 | 97 | }); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | function test_profile(aClient, aProfiler) |
michael@0 | 101 | { |
michael@0 | 102 | // No idea why, but Components.stack.sourceLine returns null. |
michael@0 | 103 | var funcLine = Components.stack.lineNumber - 3; |
michael@0 | 104 | // Busy wait a few milliseconds |
michael@0 | 105 | var start = Date.now(); |
michael@0 | 106 | var stack; |
michael@0 | 107 | while (Date.now() - start < 200) { stack = Components.stack; } |
michael@0 | 108 | aClient.request({ to: aProfiler, type: "getProfile" }, function (aResponse) { |
michael@0 | 109 | do_check_eq(typeof aResponse.profile, "object"); |
michael@0 | 110 | do_check_eq(typeof aResponse.profile.meta, "object"); |
michael@0 | 111 | do_check_eq(typeof aResponse.profile.meta.platform, "string"); |
michael@0 | 112 | do_check_eq(typeof aResponse.profile.threads, "object"); |
michael@0 | 113 | do_check_eq(typeof aResponse.profile.threads[0], "object"); |
michael@0 | 114 | do_check_eq(typeof aResponse.profile.threads[0].samples, "object"); |
michael@0 | 115 | do_check_neq(aResponse.profile.threads[0].samples.length, 0); |
michael@0 | 116 | |
michael@0 | 117 | let location = stack.name + " (" + stack.filename + ":" + funcLine + ")"; |
michael@0 | 118 | // At least one sample is expected to have been in the busy wait above. |
michael@0 | 119 | do_check_true(aResponse.profile.threads[0].samples.some(function(sample) { |
michael@0 | 120 | return typeof sample.frames == "object" && |
michael@0 | 121 | sample.frames.length != 0 && |
michael@0 | 122 | sample.frames.some(function(f) { |
michael@0 | 123 | return (f.line == stack.lineNumber) && |
michael@0 | 124 | (f.location == location); |
michael@0 | 125 | }); |
michael@0 | 126 | })); |
michael@0 | 127 | |
michael@0 | 128 | aClient.request({ to: aProfiler, type: "stopProfiler" }, function (aResponse) { |
michael@0 | 129 | do_check_eq(typeof aResponse.msg, "string"); |
michael@0 | 130 | aClient.request({ to: aProfiler, type: "isActive" }, function (aResponse) { |
michael@0 | 131 | do_check_false(aResponse.isActive); |
michael@0 | 132 | aClient.close(function() { |
michael@0 | 133 | test_profiler_status(); |
michael@0 | 134 | }); |
michael@0 | 135 | }); |
michael@0 | 136 | }); |
michael@0 | 137 | }); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | function test_profiler_status() |
michael@0 | 141 | { |
michael@0 | 142 | var connectionClosed = DebuggerServer._connectionClosed; |
michael@0 | 143 | var client = new DebuggerClient(DebuggerServer.connectPipe()); |
michael@0 | 144 | |
michael@0 | 145 | client.connect(() => { |
michael@0 | 146 | client.listTabs((aResponse) => { |
michael@0 | 147 | DebuggerServer._connectionClosed = function (conn) { |
michael@0 | 148 | connectionClosed.call(this, conn); |
michael@0 | 149 | |
michael@0 | 150 | // Check that closing the last (only?) connection stops the profiler. |
michael@0 | 151 | do_check_false(Profiler.IsActive()); |
michael@0 | 152 | do_test_finished(); |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | var profiler = aResponse.profilerActor; |
michael@0 | 156 | do_check_false(Profiler.IsActive()); |
michael@0 | 157 | client.request({ |
michael@0 | 158 | to: profiler, |
michael@0 | 159 | type: "startProfiler", |
michael@0 | 160 | features: [] |
michael@0 | 161 | }, function (aResponse) { |
michael@0 | 162 | do_check_true(Profiler.IsActive()); |
michael@0 | 163 | client.close(); |
michael@0 | 164 | }); |
michael@0 | 165 | }); |
michael@0 | 166 | }); |
michael@0 | 167 | } |