|
| 1 | +/* global TVSource, TVChannel, TVProgram */ |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +/** |
| 6 | + * This mock service contains mock channels and programs that make testing |
| 7 | + * easier. |
| 8 | + */ |
| 9 | +(function(exports) { |
| 10 | + |
| 11 | + var channelInfo = [ |
| 12 | + { |
| 13 | + name: 'HBO', |
| 14 | + number: '1' |
| 15 | + }, { |
| 16 | + name: 'TVBS', |
| 17 | + number: '2' |
| 18 | + }, { |
| 19 | + name: 'Discovery', |
| 20 | + number: '3' |
| 21 | + }, { |
| 22 | + name: 'Fox', |
| 23 | + number: '5' |
| 24 | + }, { |
| 25 | + name: 'ABC', |
| 26 | + number: '8' |
| 27 | + }, { |
| 28 | + name: 'CNN', |
| 29 | + number: '9' |
| 30 | + }, { |
| 31 | + name: 'AXS TV', |
| 32 | + number: '10' |
| 33 | + }, { |
| 34 | + name: 'MTV', |
| 35 | + number: '10-1' |
| 36 | + }, { |
| 37 | + name: 'TNT', |
| 38 | + number: '10-2' |
| 39 | + }, { |
| 40 | + name: 'Disney', |
| 41 | + number: '13' |
| 42 | + }, { |
| 43 | + name: 'Oxygen', |
| 44 | + number: '14' |
| 45 | + }, { |
| 46 | + name: 'Lifetime', |
| 47 | + number: '15' |
| 48 | + }, { |
| 49 | + name: 'AXN', |
| 50 | + number: '19' |
| 51 | + }, { |
| 52 | + name: 'ESON', |
| 53 | + number: '20' |
| 54 | + }, { |
| 55 | + name: 'Mozilla', |
| 56 | + number: '25' |
| 57 | + } |
| 58 | + ]; |
| 59 | + |
| 60 | + (function overrideTVSourceFunction() { |
| 61 | + // Override native TVChannel constructor in order to create mock channels. |
| 62 | + window.TVChannel = function() {}; |
| 63 | + |
| 64 | + // Delete native property in order to override it |
| 65 | + delete TVSource.prototype.currentChannel; |
| 66 | + |
| 67 | + var channels = []; |
| 68 | + for(var i = 0; i < channelInfo.length; i++) { |
| 69 | + var channel = new TVChannel(); |
| 70 | + channel.name = channelInfo[i].name; |
| 71 | + channel.number = channelInfo[i].number; |
| 72 | + channels.push(channel); |
| 73 | + } |
| 74 | + |
| 75 | + TVSource.prototype.getChannels = function() { |
| 76 | + var lastChannel; |
| 77 | + var lastChannelNumber = localStorage.getItem('_Mock-Channel-Number'); |
| 78 | + channels.some(function(channel) { |
| 79 | + if (channel.number === lastChannelNumber) { |
| 80 | + lastChannel = channel; |
| 81 | + return true; |
| 82 | + } |
| 83 | + return false; |
| 84 | + }.bind(this)); |
| 85 | + this.currentChannel = lastChannel; |
| 86 | + return { |
| 87 | + then: function(success) { |
| 88 | + setTimeout(function() { |
| 89 | + success(channels); |
| 90 | + }, 500); |
| 91 | + } |
| 92 | + }; |
| 93 | + }; |
| 94 | + |
| 95 | + TVSource.prototype.setCurrentChannel = function(channelNumber) { |
| 96 | + var resolve; |
| 97 | + if (this._lastTimeoutId) { |
| 98 | + clearTimeout(this._lastTimeoutId); |
| 99 | + } |
| 100 | + this._lastTimeoutId = setTimeout(function() { |
| 101 | + this.currentChannel = channels[channelNumber]; |
| 102 | + localStorage.setItem('_Mock-Channel-Number', channelNumber); |
| 103 | + if (resolve) { |
| 104 | + resolve(); |
| 105 | + } |
| 106 | + }.bind(this), 500); |
| 107 | + |
| 108 | + return { |
| 109 | + then: function(success) { |
| 110 | + resolve = success; |
| 111 | + } |
| 112 | + }; |
| 113 | + }; |
| 114 | + })(); |
| 115 | + |
| 116 | + var programTitles = [ |
| 117 | + 'The Dark Knight', |
| 118 | + 'Pulp Fiction', |
| 119 | + 'The Lord of the Rings: The Return of the King', |
| 120 | + 'Star Wars: Episode V - The Empire Strikes Back', |
| 121 | + 'Forrest Gump', |
| 122 | + 'Inception', |
| 123 | + 'The Matrix', |
| 124 | + 'Interstellar', |
| 125 | + 'Whiplash', |
| 126 | + 'The Intouchables', |
| 127 | + 'Gladiator', |
| 128 | + 'Alien ', |
| 129 | + 'The Lion King', |
| 130 | + 'American Beauty', |
| 131 | + 'Toy Story 3', |
| 132 | + 'Indiana Jones and the Last Crusade', |
| 133 | + 'The Wolf of Wall Street', |
| 134 | + 'Gone Girl', |
| 135 | + 'A Beautiful Mind', |
| 136 | + 'The Grand Budapest Hotel', |
| 137 | + 'Monsters, Inc.', |
| 138 | + 'The Hobbit: The Battle of the Five Armies', |
| 139 | + 'Maleficent', |
| 140 | + 'X-Men: Days of Future Past', |
| 141 | + 'Captain America: The Winter Soldier', |
| 142 | + 'The Amazing Spider-Man 2', |
| 143 | + 'Dawn of the Planet of the Apes', |
| 144 | + 'Fast & Furious 6', |
| 145 | + 'Iron Man 3', |
| 146 | + 'Frozen', |
| 147 | + '2012', |
| 148 | + 'Magic Mike', |
| 149 | + 'The Hunger Games', |
| 150 | + 'Dredd', |
| 151 | + 'Silver Linings Playbook', |
| 152 | + 'The Hobbit: An Unexpected Journey', |
| 153 | + '21 Jump Street', |
| 154 | + 'The Amazing Spider-Man', |
| 155 | + 'Skyfall', |
| 156 | + 'Argo' |
| 157 | + ]; |
| 158 | + |
| 159 | + (function overrideTVChannelFunction() { |
| 160 | + // Override native TVProgram constructor in order to create mock programs. |
| 161 | + window.TVProgram = function() {}; |
| 162 | + |
| 163 | + TVChannel.prototype._generatePrograms = function() { |
| 164 | + var quarterHour = 15 * 60000; // 15 min |
| 165 | + var halfHour = 30 * 60000; |
| 166 | + var startTime = |
| 167 | + new Date((Math.floor(Date.now() / halfHour) - 10) * halfHour) |
| 168 | + .getTime(); |
| 169 | + var duration; |
| 170 | + var programs = []; |
| 171 | + var program; |
| 172 | + var index; |
| 173 | + var tmp; |
| 174 | + var i; |
| 175 | + |
| 176 | + for(i = 0; i < programTitles.length; i++) { |
| 177 | + index = Math.floor(Math.random() * (programTitles.length - i)) + i; |
| 178 | + tmp = programTitles[i]; |
| 179 | + programTitles[i] = programTitles[index]; |
| 180 | + programTitles[index] = tmp; |
| 181 | + duration = (Math.floor(Math.random() * 4) + 2 ) * quarterHour; |
| 182 | + |
| 183 | + program = new TVProgram(); |
| 184 | + program.title = programTitles[i]; |
| 185 | + program.startTime = startTime; |
| 186 | + program.duration = duration; |
| 187 | + programs.push(program); |
| 188 | + startTime += Math.ceil(duration / halfHour) * halfHour; |
| 189 | + |
| 190 | + // Add random null program slot |
| 191 | + if (Math.random() < 0.2) { |
| 192 | + startTime += halfHour; |
| 193 | + } |
| 194 | + } |
| 195 | + this._programs = programs; |
| 196 | + }; |
| 197 | + |
| 198 | + TVChannel.prototype.getPrograms = function() { |
| 199 | + return { |
| 200 | + then: function(success) { |
| 201 | + if (!this._programs) { |
| 202 | + this._generatePrograms(); |
| 203 | + } |
| 204 | + setTimeout(function() { |
| 205 | + success(this._programs); |
| 206 | + }.bind(this), 800); |
| 207 | + }.bind(this) |
| 208 | + }; |
| 209 | + }; |
| 210 | + })(); |
| 211 | +})(window); |
| 212 | + |
0 commit comments