Simplify the test harness
[WebKit-https.git] / PerformanceTests / Animometer / tests / bouncing-particles / resources / bouncing-svg-shapes.js
1 (function() {
2
3 BouncingSvgShape = Utilities.createSubclass(BouncingSvgParticle,
4     function(stage)
5     {
6         BouncingSvgParticle.call(this, stage, stage.shape);
7         this._fill = stage.fill;
8
9         this._createShape(stage);
10         this._applyClipping(stage);
11         this._applyFill(stage);
12
13         this._move();
14     }, {
15
16     _createShape: function(stage)
17     {
18         switch (this._shape) {
19         case "rect":
20             var attrs = { x: 0, y: 0, width: this.size.x, height: this.size.y };
21             this.element = DocumentExtension.createSvgElement("rect", attrs, {}, stage.element);
22             break;
23
24         case "circle":
25         default:
26             var attrs = { cx: this.size.x / 2, cy: this.size.y / 2, r: Math.min(this.size.x, this.size.y) / 2 };
27             this.element = DocumentExtension.createSvgElement("circle", attrs, {}, stage.element);
28             break;
29         }
30     },
31
32     _applyFill: function(stage)
33     {
34         switch (this._fill) {
35         case "gradient":
36             var gradient = stage.createGradient(2);
37             this.element.setAttribute("fill", "url(#" + gradient.getAttribute("id") + ")");
38             break;
39
40         case "solid":
41         default:
42             this.element.setAttribute("fill", stage.randomColor());
43             break;
44         }
45     }
46 });
47
48 BouncingSvgShapesStage = Utilities.createSubclass(BouncingSvgParticlesStage,
49     function()
50     {
51         BouncingSvgParticlesStage.call(this);
52     }, {
53
54     initialize: function(benchmark)
55     {
56         BouncingSvgParticlesStage.prototype.initialize.call(this, benchmark);
57         this.parseShapeParameters(benchmark.options);
58         this._gradientsCount = 0;
59     },
60
61     createGradient: function(stops)
62     {
63         var attrs = { id: "gradient-" + ++this._gradientsCount };
64         var gradient = DocumentExtension.createSvgElement("linearGradient", attrs, {}, this._ensureDefsIsCreated());
65
66         for (var i = 0; i < stops; ++i) {
67             attrs = { offset: i * 100 / stops + "%", 'stop-color': this.randomColor() };
68             DocumentExtension.createSvgElement("stop", attrs, {}, gradient);
69         }
70
71         return gradient;
72     },
73
74     createParticle: function()
75     {
76         return new BouncingSvgShape(this);
77     },
78
79     particleWillBeRemoved: function(particle)
80     {
81         BouncingSvgParticlesStage.prototype.particleWillBeRemoved.call(this, particle);
82
83         var fill = particle.element.getAttribute("fill");
84         if (fill.indexOf("url(#") != 0)
85             return;
86
87         var gradient = this.element.querySelector(fill.substring(4, fill.length - 1));
88         this._ensureDefsIsCreated().removeChild(gradient);
89     }
90 });
91
92 BouncingSvgShapesBenchmark = Utilities.createSubclass(Benchmark,
93     function(options)
94     {
95         Benchmark.call(this, new BouncingSvgShapesStage(), options);
96     }
97 );
98
99 window.benchmarkClass = BouncingSvgShapesBenchmark;
100
101 })();