Home | History | Annotate | Download | only in trace_model
      1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 'use strict';
      6 
      7 base.require('tracing.test_utils');
      8 base.require('tracing.trace_model.object_collection');
      9 
     10 base.unittest.testSuite('tracing.trace_model.object_collection', function() {
     11   var TestObjectInstance = function(parent, id, category, name, creationTs) {
     12     tracing.trace_model.ObjectInstance.call(
     13         this, parent, id, category, name, creationTs);
     14   };
     15 
     16   TestObjectInstance.prototype = {
     17     __proto__: tracing.trace_model.ObjectInstance.prototype
     18   };
     19 
     20   test('objectInstanceSubtype', function() {
     21     // Register that TestObjects are bound to TestObjectInstance.
     22     tracing.trace_model.ObjectInstance.register(
     23         'TestObject', TestObjectInstance);
     24     try {
     25       var collection = new tracing.trace_model.ObjectCollection({ });
     26       collection.idWasCreated(
     27           '0x1000', 'cc', 'Frame', 10);
     28       collection.idWasDeleted(
     29           '0x1000', 'cc', 'Frame', 15);
     30       collection.idWasCreated(
     31           '0x1000', 'skia', 'TestObject', 20);
     32       collection.idWasDeleted(
     33           '0x1000', 'skia', 'TestObject', 25);
     34 
     35       var testFrame = collection.getObjectInstanceAt('0x1000', 10);
     36       assertTrue(testFrame instanceof tracing.trace_model.ObjectInstance);
     37       assertFalse(testFrame instanceof TestObjectInstance);
     38 
     39       var testObject = collection.getObjectInstanceAt('0x1000', 20);
     40       assertTrue(testObject instanceof tracing.trace_model.ObjectInstance);
     41       assertTrue(testObject instanceof TestObjectInstance);
     42     } finally {
     43       tracing.trace_model.ObjectInstance.unregister('TestObject');
     44     }
     45   });
     46 
     47   test('twoSnapshots', function() {
     48     var collection = new tracing.trace_model.ObjectCollection({});
     49     collection.idWasCreated(
     50         '0x1000', 'cat', 'Frame', 10);
     51     collection.addSnapshot(
     52         '0x1000', 'cat', 'Frame', 10, {foo: 1});
     53     collection.addSnapshot(
     54         '0x1000', 'cat', 'Frame', 20, {foo: 2});
     55 
     56     collection.updateBounds();
     57     assertEquals(10, collection.bounds.min);
     58     assertEquals(20, collection.bounds.max);
     59 
     60     var s0 = collection.getSnapshotAt('0x1000', 1);
     61     assertUndefined(s0);
     62 
     63     var s1 = collection.getSnapshotAt('0x1000', 10);
     64     assertEquals(s1.args.foo, 1);
     65 
     66     var s2 = collection.getSnapshotAt('0x1000', 15);
     67     assertEquals(s2.args.foo, 1);
     68     assertEquals(s1, s2);
     69 
     70     var s3 = collection.getSnapshotAt('0x1000', 20);
     71     assertEquals(s3.args.foo, 2);
     72     assertEquals(s1.object, s3.object);
     73 
     74     var s4 = collection.getSnapshotAt('0x1000', 25);
     75     assertEquals(s4, s3);
     76   });
     77 
     78   test('twoObjectsSharingOneID', function() {
     79     var collection = new tracing.trace_model.ObjectCollection({});
     80     collection.idWasCreated(
     81         '0x1000', 'cc', 'Frame', 10);
     82     collection.idWasDeleted(
     83         '0x1000', 'cc', 'Frame', 15);
     84     collection.idWasCreated(
     85         '0x1000', 'skia', 'Picture', 20);
     86     collection.idWasDeleted(
     87         '0x1000', 'skia', 'Picture', 25);
     88 
     89     var frame = collection.getObjectInstanceAt('0x1000', 10);
     90     assertEquals('cc', frame.category);
     91     assertEquals('Frame', frame.name);
     92 
     93     var picture = collection.getObjectInstanceAt('0x1000', 20);
     94     assertEquals('skia', picture.category);
     95     assertEquals('Picture', picture.name);
     96 
     97     var typeNames = base.dictionaryKeys(collection.getAllInstancesByTypeName());
     98     typeNames.sort();
     99     assertArrayEquals(
    100         ['Frame', 'Picture'],
    101         typeNames);
    102     assertArrayEquals(
    103         [frame],
    104         collection.getAllInstancesByTypeName()['Frame']);
    105     assertArrayEquals(
    106         [picture],
    107         collection.getAllInstancesByTypeName()['Picture']);
    108   });
    109 
    110   test('createSnapDelete', function() {
    111     var collection = new tracing.trace_model.ObjectCollection({});
    112     collection.idWasCreated(
    113         '0x1000', 'cat', 'Frame', 10);
    114     collection.addSnapshot(
    115         '0x1000', 'cat', 'Frame', 10, {foo: 1});
    116     collection.idWasDeleted(
    117         '0x1000', 'cat', 'Frame', 15);
    118 
    119     collection.updateBounds();
    120     assertEquals(10, collection.bounds.min);
    121     assertEquals(15, collection.bounds.max);
    122 
    123     var s10 = collection.getSnapshotAt('0x1000', 10);
    124     var i10 = s10.objectInstance;
    125     assertEquals(10, i10.creationTs);
    126     assertEquals(15, i10.deletionTs);
    127   });
    128 
    129   test('boundsOnUndeletedObject', function() {
    130     var collection = new tracing.trace_model.ObjectCollection({});
    131     collection.idWasCreated(
    132         '0x1000', 'cat', 'Frame', 10);
    133     collection.addSnapshot(
    134         '0x1000', 'cat', 'Frame', 15, {foo: 1});
    135 
    136     collection.updateBounds();
    137     assertEquals(collection.bounds.min, 10);
    138     assertEquals(collection.bounds.max, 15);
    139   });
    140 
    141   test('autoDelete', function() {
    142     var collection = new tracing.trace_model.ObjectCollection({});
    143     collection.idWasCreated(
    144         '0x1000', 'cat', 'Frame', 10);
    145     collection.addSnapshot(
    146         '0x1000', 'cat', 'Frame', 10, {foo: 1});
    147     collection.autoDeleteObjects(15);
    148 
    149     var s10 = collection.getSnapshotAt('0x1000', 10);
    150     var i10 = s10.objectInstance;
    151     assertEquals(i10.deletionTs, 15);
    152   });
    153 });
    154