Skip to content

Commit

Permalink
[Time Conductor] Adding tests and fixing failing ones. #933
Browse files Browse the repository at this point in the history
  • Loading branch information
akhenry committed Sep 6, 2016
1 parent 4cf6126 commit c6eaa3d
Show file tree
Hide file tree
Showing 22 changed files with 1,333 additions and 129 deletions.
10 changes: 5 additions & 5 deletions platform/commonUI/formats/src/UTCTimeFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ define([
* @private
*/
function getScaledFormat (d) {
var m = moment.utc(d);
var momentified = moment.utc(d);
/**
* Uses logic from d3 Time-Scales, v3 of the API. See
* https://github.com/d3/d3-3.x-api-reference/blob/master/Time-Scales.md
Expand All @@ -71,17 +71,17 @@ define([
["HH", function(m) { return m.hours(); }],
["ddd DD", function(m) {
return m.days() &&
m.date() != 1;
m.date() !== 1;
}],
["MMM DD", function(m) { return m.date() != 1; }],
["MMM DD", function(m) { return m.date() !== 1; }],
["MMMM", function(m) {
return m.month();
}],
["YYYY", function() { return true; }]
].filter(function (row){
return row[1](m);
return row[1](momentified);
})[0][0];
};
}

/**
*
Expand Down
62 changes: 62 additions & 0 deletions platform/commonUI/formats/src/UTCTimeFormatSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*****************************************************************************
* Open MCT Web, Copyright (c) 2014-2015, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT Web includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/

define([
"./UTCTimeFormat",
"moment"
], function (
UTCTimeFormat,
moment
) {
describe("The UTCTimeFormat class", function () {
var format;
var scale;

beforeEach(function () {
format = new UTCTimeFormat();
scale = {min: 0, max: 0};
});

it("Provides an appropriately scaled time format based on the input" +
" time", function () {
var TWO_HUNDRED_MS = 200;
var THREE_SECONDS = 3000;
var FIVE_MINUTES = 5 * 60 * 1000;
var ONE_HOUR_TWENTY_MINS = (1 * 60 * 60 * 1000) + (20 * 60 * 1000);
var TEN_HOURS = (10 * 60 * 60 * 1000);

var JUNE_THIRD = moment.utc("2016-06-03", "YYYY-MM-DD");
var APRIL = moment.utc("2016-04", "YYYY-MM");
var TWENTY_SIXTEEN = moment.utc("2016", "YYYY");

expect(format.format(TWO_HUNDRED_MS, scale)).toBe(".200");
expect(format.format(THREE_SECONDS, scale)).toBe(":03");
expect(format.format(FIVE_MINUTES, scale)).toBe("00:05");
expect(format.format(ONE_HOUR_TWENTY_MINS, scale)).toBe("01:20");
expect(format.format(TEN_HOURS, scale)).toBe("10");

expect(format.format(JUNE_THIRD, scale)).toBe("Fri 03");
expect(format.format(APRIL, scale)).toBe("April");
expect(format.format(TWENTY_SIXTEEN, scale)).toBe("2016");
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ define(

this.conductor.on("bounds", this.boundsListener);
this.conductor.on("timeSystem", this.timeSystemListener);
this.conductor.on("follow", this.followListener)
this.conductor.on("follow", this.followListener);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ define([

ConductorService.prototype.getConductor = function () {
return this.tc;
}
};

return ConductorService;
});
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ define(
return function() {
unsubscribeFunc();
conductor.off('bounds', amendRequests);
}
};
};

return ConductorTelemetryDecorator;
Expand Down
1 change: 1 addition & 0 deletions platform/features/conductor-v2/conductor/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ define([
"implementation": TimeConductorController,
"depends": [
"$scope",
"$window",
"timeConductor",
"timeConductorViewService",
"timeSystems[]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ define(['./TimeConductor'], function (TimeConductor) {

it("Allows setting of valid bounds", function () {
bounds = {start: 0, end: 1};
expect(tc.bounds()).not.toBe(bounds);
expect(tc.bounds()).not.toEqual(bounds);
expect(tc.bounds.bind(tc, bounds)).not.toThrow();
expect(tc.bounds()).toBe(bounds);
expect(tc.bounds()).toEqual(bounds);
});

it("Disallows setting of invalid bounds", function () {
bounds = {start: 1, end: 0};
expect(tc.bounds()).not.toBe(bounds);
expect(tc.bounds()).not.toEqual(bounds);
expect(tc.bounds.bind(tc, bounds)).toThrow();
expect(tc.bounds()).not.toBe(bounds);
expect(tc.bounds()).not.toEqual(bounds);

bounds = {start: 1};
expect(tc.bounds()).not.toBe(bounds);
expect(tc.bounds()).not.toEqual(bounds);
expect(tc.bounds.bind(tc, bounds)).toThrow();
expect(tc.bounds()).not.toBe(bounds);
expect(tc.bounds()).not.toEqual(bounds);
});

it("Allows setting of time system with bounds", function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*****************************************************************************
* Open MCT Web, Copyright (c) 2014-2015, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT Web includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/

define(["./LocalClock"], function (LocalClock) {
describe("The LocalClock class", function () {
var clock,
mockTimeout,
timeoutHandle = {};

beforeEach(function () {
mockTimeout = jasmine.createSpy("timeout");
mockTimeout.andReturn(timeoutHandle);
mockTimeout.cancel = jasmine.createSpy("cancel");

clock = new LocalClock(mockTimeout, 0);
clock.start();
});

it("calls listeners on tick with current time", function () {
var mockListener = jasmine.createSpy("listener");
clock.listen(mockListener);
clock.tick();
expect(mockListener).toHaveBeenCalledWith(jasmine.any(Number));
});

it("stops ticking when stop is called", function () {
clock.stop();
expect(mockTimeout.cancel).toHaveBeenCalledWith(timeoutHandle);
});
});
});
164 changes: 89 additions & 75 deletions platform/features/conductor-v2/conductor/src/ui/MctConductorAxis.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,99 +22,113 @@

define(
[
"zepto",
"d3"
],
function ($, d3) {
function (d3) {
var PADDING = 1;

/**
* The mct-conductor-axis renders a horizontal axis with regular
* labelled 'ticks'. It requires 'start' and 'end' integer values to
* be specified as attributes.
*/
function MCTConductorAxis(conductor, formatService) {
function link(scope, element, attrs, ngModelController) {
var target = element[0].firstChild,
height = target.offsetHeight,
padding = 1;

var vis = d3.select(target)
.append('svg:svg')
.attr('width', '100%')
.attr('height', height);
var xAxis = d3.axisTop();
var xScale = d3.scaleUtc();

// draw x axis with labels and move to the bottom of the chart area
var axisElement = vis.append("g")
.attr("transform", "translate(0," + (height - padding) + ")");

function setScale() {
var width = target.offsetWidth;
var timeSystem = conductor.timeSystem();
var bounds = conductor.bounds();

if (timeSystem.isUTCBased()) {
xScale.domain([new Date(bounds.start), new Date(bounds.end)]);
} else {
xScale.domain([bounds.start, bounds.end]);
}
// Dependencies
this.d3 = d3;
this.conductor = conductor;
this.formatService = formatService;

// Runtime properties (set by 'link' function)
this.target = undefined;
this.xScale = undefined;
this.xAxis = undefined;
this.axisElement = undefined;
this.setScale = this.setScale.bind(this);
this.changeTimeSystem = this.changeTimeSystem.bind(this);

// Angular Directive interface
this.link = this.link.bind(this);
this.restrict = "E";
this.template =
"<div class=\"l-axis-holder\" mct-resize=\"resize()\"></div>";
this.priority = 1000;
}

MCTConductorAxis.prototype.setScale = function () {
var width = this.target.offsetWidth;
var timeSystem = this.conductor.timeSystem();
var bounds = this.conductor.bounds();

if (timeSystem.isUTCBased()) {
this.xScale = this.xScale || this.d3.scaleUtc();
this.xScale.domain([new Date(bounds.start), new Date(bounds.end)]);
} else {
this.xScale = this.xScale || this.d3.scaleLinear();
this.xScale.domain([bounds.start, bounds.end]);
}

xScale.range([padding, width - padding * 2]);
axisElement.call(xAxis);
this.xScale.range([PADDING, width - PADDING * 2]);
this.axisElement.call(this.xAxis);
};

MCTConductorAxis.prototype.changeTimeSystem = function (timeSystem) {
var key = timeSystem.formats()[0];
if (key !== undefined) {
var format = this.formatService.getFormat(key);
var bounds = this.conductor.bounds();

if (timeSystem.isUTCBased()) {
this.xScale = this.d3.scaleUtc();
} else {
this.xScale = this.d3.scaleLinear();
}

function changeTimeSystem(timeSystem) {
var key = timeSystem.formats()[0];
if (key !== undefined) {
var format = formatService.getFormat(key);
var b = conductor.bounds();

if (timeSystem.isUTCBased()) {
xScale = d3.scaleUtc();
} else {
xScale = d3.scaleLinear();
}

xAxis.scale(xScale);
//Define a custom format function
xAxis.tickFormat(function (tickValue) {
// Normalize date representations to numbers
if (tickValue instanceof Date){
tickValue = tickValue.getTime();
}
return format.format(tickValue, {
min: b.start,
max: b.end
});
});
axisElement.call(xAxis);
this.xAxis.scale(this.xScale);
//Define a custom format function
this.xAxis.tickFormat(function (tickValue) {
// Normalize date representations to numbers
if (tickValue instanceof Date){
tickValue = tickValue.getTime();
}
}
return format.format(tickValue, {
min: bounds.start,
max: bounds.end
});
});
this.axisElement.call(this.xAxis);
}
};

scope.resize = setScale;
MCTConductorAxis.prototype.link = function (scope, element) {
var conductor = this.conductor;
this.target = element[0].firstChild;
var height = this.target.offsetHeight;
var vis = this.d3.select(this.target)
.append('svg:svg')
.attr('width', '100%')
.attr('height', height);

conductor.on('timeSystem', changeTimeSystem);
this.xAxis = this.d3.axisTop();

//On conductor bounds changes, redraw ticks
conductor.on('bounds', function (bounds) {
setScale();
});
// draw x axis with labels and move to the bottom of the chart area
this.axisElement = vis.append("g")
.attr("transform", "translate(0," + (height - PADDING) + ")");

if (conductor.timeSystem() !== undefined) {
changeTimeSystem(conductor.timeSystem());
setScale();
}
}
scope.resize = this.setScale;

return {
restrict: "E",
template: "<div class=\"l-axis-holder\" mct-resize=\"resize()\"></div>",
priority: 1000,
link: link
};
}
conductor.on('timeSystem', this.changeTimeSystem);

//On conductor bounds changes, redraw ticks
conductor.on('bounds', this.setScale);

if (conductor.timeSystem() !== undefined) {
this.changeTimeSystem(conductor.timeSystem());
this.setScale();
}
};

return MCTConductorAxis;
return function(conductor, formatService) {
return new MCTConductorAxis(conductor, formatService);
};
}
);
Loading

0 comments on commit c6eaa3d

Please sign in to comment.