1# Copyright 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
5from branch_utility import BranchUtility, ChannelInfo
6from test_data.canned_data import (CANNED_BRANCHES, CANNED_CHANNELS)
7
8
9class TestBranchUtility(object):
10  '''Mimics BranchUtility to return valid-ish data without needing omahaproxy
11  data.
12  '''
13
14  def __init__(self, versions, channels):
15    ''' Parameters: |version| is a mapping of versions to branches, and
16    |channels| is a mapping of channels to versions.
17    '''
18    self._versions = versions
19    self._channels = channels
20
21  @staticmethod
22  def CreateWithCannedData():
23    '''Returns a TestBranchUtility that uses 'canned' test data pulled from
24    older branches of SVN data.
25    '''
26    return TestBranchUtility(CANNED_BRANCHES, CANNED_CHANNELS)
27
28  def GetAllChannelInfo(self):
29    return tuple(self.GetChannelInfo(channel)
30            for channel in BranchUtility.GetAllChannelNames())
31
32  def GetChannelInfo(self, channel):
33    version = self._channels[channel]
34    return ChannelInfo(channel, self.GetBranchForVersion(version), version)
35
36  def GetStableChannelInfo(self, version):
37    return ChannelInfo('stable', self.GetBranchForVersion(version), version)
38
39  def GetBranchForVersion(self, version):
40    return self._versions[version]
41
42  def GetChannelForVersion(self, version):
43    if version <= self._channels['stable']:
44      return 'stable'
45    for channel in self._channels.iterkeys():
46      if self._channels[channel] == version:
47        return channel
48
49  def Older(self, channel_info):
50    versions = self._versions.keys()
51    index = versions.index(channel_info.version)
52    if index == len(versions) - 1:
53      return None
54    version = versions[index + 1]
55    return ChannelInfo(self.GetChannelForVersion(version),
56                       self.GetBranchForVersion(version),
57                       version)
58
59  def Newer(self, channel_info):
60    versions = self._versions.keys()
61    index = versions.index(channel_info.version)
62    if not index:
63      return None
64    version = versions[index - 1]
65    return ChannelInfo(self.GetChannelForVersion(version),
66                       self.GetBranchForVersion(version),
67                       version)
68