6e07475071870d8f4cfae95794d7d28c46037649
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / inspector / profiler / heap-snapshot-loader.html
1 <html>
2 <head>
3 <script src="../../http/tests/inspector/inspector-test.js"></script>
4 <script src="heap-snapshot-test.js"></script>
5 <script>
6
7 function test()
8 {
9     WebInspector.inspectorView.showPanel("profiles");
10     var source = InspectorTest.createHeapSnapshotMockRaw();
11     var sourceStringified = JSON.stringify(source);
12     var partSize = sourceStringified.length >> 3;
13
14     function injectMockProfile(callback) {
15         var dispatcher = InspectorBackend._connection._dispatchers["HeapProfiler"]._dispatcher;
16         var panel = WebInspector.panels.profiles;
17         panel._reset();
18
19         var profileType =  WebInspector.ProfileTypeRegistry.instance.heapSnapshotProfileType;
20
21         InspectorTest.override(HeapProfilerAgent, "takeHeapSnapshot", takeHeapSnapshotMock);
22         function takeHeapSnapshotMock(reportProgress, callback) {
23             if (reportProgress) {
24                 profileType._reportHeapSnapshotProgress({data: {done: 50, total: 100, finished: false}});
25                 profileType._reportHeapSnapshotProgress({data: {done: 100, total: 100, finished: true}});
26             }
27             for (var i = 0, l = sourceStringified.length; i < l; i += partSize)
28                 dispatcher.addHeapSnapshotChunk(sourceStringified.slice(i, i + partSize));
29             setTimeout(callback, 0);
30         }
31
32         function tempFileReady()
33         {
34             callback(this);
35         }
36         InspectorTest.addSniffer(WebInspector.HeapProfileHeader.prototype, "_didWriteToTempFile", tempFileReady);
37         profileType._takeHeapSnapshot(function() {});
38     }
39
40     WebInspector.console.log = function(message) {
41         InspectorTest.addResult("WebInspector.consoleModel.log: " + message);
42     }
43
44     InspectorTest.runTestSuite([
45         function heapSnapshotSaveToFileTest(next)
46         {
47             function snapshotLoaded(profileHeader)
48             {
49                 var savedSnapshotData;
50                 function saveMock(url, data)
51                 {
52                     savedSnapshotData = data;
53                     WebInspector.fileManager._savedURL({data: url});
54                 }
55                 InspectorTest.override(InspectorFrontendHost, "save", saveMock);
56
57                 var oldAppend = InspectorFrontendHost.append;
58                 InspectorFrontendHost.append = function appendMock(url, data)
59                 {
60                     savedSnapshotData += data;
61                     WebInspector.fileManager._appendedToURL({data: url});
62                 }
63                 function closeMock(url)
64                 {
65                     InspectorTest.assertEquals(sourceStringified, savedSnapshotData, "Saved snapshot data");
66                     InspectorFrontendHost.append = oldAppend;
67                     next();
68                 }
69                 InspectorTest.override(WebInspector.FileManager.prototype, "close", closeMock);
70                 profileHeader.saveToFile();
71             }
72
73             injectMockProfile(snapshotLoaded);
74         },
75
76         function heapSnapshotLoadFromFileTest(next)
77         {
78             var panel = WebInspector.panels.profiles;
79
80             var fileMock = {
81                 name: "mock.heapsnapshot",
82                 size: sourceStringified.length
83             };
84
85             InspectorTest.override(WebInspector.HeapProfileHeader.prototype, '_createFileReader', function(fileMock, delegate) {
86                 return {
87                     start: function(receiver) {
88                         delegate.onTransferStarted(this);
89                         receiver.write(sourceStringified);
90                         delegate.onChunkTransferred(this);
91                         receiver.close();
92                         delegate.onTransferFinished(this);
93                     },
94
95                     loadedSize: function()
96                     {
97                         return fileMock.size;
98                     },
99
100                     fileSize: function()
101                     {
102                         return fileMock.size;
103                     },
104
105                     fileName: function()
106                     {
107                         return fileMock.name;
108                     }
109                 };
110             });
111             InspectorTest.addSniffer(WebInspector.HeapProfileHeader.prototype, "_snapshotReceived", function() { next(); });
112             panel._loadFromFile(fileMock);
113         },
114
115         function heapSnapshotRejectToSaveToFileTest(next)
116         {
117             function snapshotLoaded(profileHeader)
118             {
119                 if (profileHeader.canSaveToFile())
120                     next();
121                 else
122                     profileHeader.addEventListener(WebInspector.ProfileHeader.Events.ProfileReceived, onCanSaveProfile, this);
123                 function onCanSaveProfile()
124                 {
125                     InspectorTest.assertTrue(profileHeader.canSaveToFile());
126                     next();
127                 }
128             }
129
130             injectMockProfile(snapshotLoaded);
131         }
132     ]);
133 }
134
135 </script>
136 </head>
137
138 <body onload="runTest()">
139 <p>
140 This test checks HeapSnapshots loader.
141 </p>
142
143 </body>
144 </html>