Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(248)

Unified Diff: Tools/GardeningServer/lib/test/util-tests.html

Issue 526633002: Apply object updates from the network without blowing away object identity or UI attributes. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Initial Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Tools/GardeningServer/lib/test/util-tests.html
diff --git a/Tools/GardeningServer/lib/test/util-tests.html b/Tools/GardeningServer/lib/test/util-tests.html
new file mode 100644
index 0000000000000000000000000000000000000000..b2f2e4bf45f9d2a872268163819c1a6b4d7ea44f
--- /dev/null
+++ b/Tools/GardeningServer/lib/test/util-tests.html
@@ -0,0 +1,135 @@
+<!--
+Copyright 2014 The Chromium Authors. All rights reserved.
+Use of this source code is governed by a BSD-style license that can be
+found in the LICENSE file.
+-->
+
+<link rel="import" href="../util.html">
+
+<script>
+
+(function () {
+'use strict';
+
+var assert = chai.assert;
+
+describe('util', function() {
+ describe('.updateLeft', function() {
+ it('updates an array', function() {
+ var x = {fingerprint: 'x', val: 0};
+ var y1 = {fingerprint: 'y', val: 1};
+ var y2 = {fingerprint: 'y', val: 2};
+ var z = {fingerprint: 'z', val: 3};
+ var target = [x, y1];
+ var source = [y2, z];
+ util.updateLeft(target, source);
+ assert.strictEqual(target[0], y1);
+ assert.strictEqual(target[1], z);
+ assert.equal(target[0].val, 2);
+ });
+
+ it('updates and reorders an array', function() {
+ var x = {fingerprint: 'x', val: 0};
+ var y1 = {fingerprint: 'y', val: 1};
+ var y2 = {fingerprint: 'y', val: 2};
+ var z = {fingerprint: 'z', val: 3};
+ var target = [x, y1];
+ var source = [z, y2];
+ util.updateLeft(target, source);
+ assert.strictEqual(target[0], z);
+ assert.strictEqual(target[1], y1);
+ assert.equal(target[1].val, 2);
+ });
+
+ it('calls custom updateLeft members', function() {
+ var y1 = {fingerprint: 'y', val: 1,
+ updateLeft: function(right) {
+ this.val += right.val;
+ }};
+ var y2 = {fingerprint: 'y', val: 2};
+ var target = [y1];
+ var source = [y2];
+ util.updateLeft(target, source);
+ assert.strictEqual(target[0], y1);
+ assert.propertyVal(target[0], 'val', 3);
+ assert.property(target[0], 'updateLeft');
+ });
+
+ it('calls custom updateLeft members in custom types', function() {
+ function Updatable(val) {
+ this.fingerprint = 'x';
+ this.val = val;
+ };
+ Updatable.prototype.updateLeft = function(right) {
+ this.val += right.val;
+ };
+
+ var y1 = new Updatable(1);
+ var y2 = new Updatable(2);
+ var target = [y1];
+ var source = [y2];
+ util.updateLeft(target, source);
+ assert.strictEqual(target[0], y1);
+ assert.propertyVal(target[0], 'val', 3);
+ assert.property(target[0], 'updateLeft');
+ });
+
+ it('skips UI properties', function() {
+ function Updatable(val, visible) {
+ this.val = val;
+ if (visible !== undefined)
+ this.visible = visible
+ };
+ Updatable.uiStateProperties = ['visible'];
+
+ var target = new Updatable(1, true);
+ var source = new Updatable(2, false);
+ util.updateLeft(target, source);
+ assert.propertyVal(target, 'val', 2);
+ assert.propertyVal(target, 'visible', true);
+
+ var target = new Updatable(1, undefined);
+ var source = new Updatable(2, true);
+ util.updateLeft(target, source);
+ assert.notProperty(target, 'visible');
+
+ var target = new Updatable(1, true);
+ var source = new Updatable(2, undefined);
+ util.updateLeft(target, source);
+ assert.propertyVal(target, 'visible', true);
+ });
+
+ it('updates array containing null', function() {
+ var target = [{fingerprint: 'x', a: {b:1}}];
+ var source = [{fingerprint: 'x', a: null}];
+ util.updateLeft(target, source);
+ assert.propertyVal(target[0], 'a', null);
+
+ var target = [{fingerprint: 'x', a: null}];
+ var source = [{fingerprint: 'x', a: {b:1}}];
+ util.updateLeft(target, source);
+ assert.deepEqual(target[0].a, {b:1});
+ });
+
+ it('updates object containing null', function() {
+ var target = {x: {a: 1}};
+ var source = {x: null};
+ util.updateLeft(target, source);
+ assert.propertyVal(target, 'x', null);
+
+ var target = {x: null};
+ var source = {x: {a: 1}};
+ util.updateLeft(target, source);
+ assert.deepEqual(target.x, {a: 1});
+ });
+
+ it('throws on badly typed arguments', function() {
+ assert.throws(function() {util.updateLeft(1, 2)}, TypeError, '1, 2');
+ assert.throws(function() {util.updateLeft([1], [2])}, TypeError, '[1], [2]');
+ });
+ });
+});
+
+})();
+
+</script>

Powered by Google App Engine
This is Rietveld 408576698