1# Copyright 2013 The ChromiumOS Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4"""This module defines the common mock tasks used by various unit tests. 5 6Part of the Chrome build flags optimization. 7""" 8 9__author__ = "[email protected] (Yuheng Long)" 10 11# Pick an integer at random. 12POISONPILL = 975 13 14 15class MockTask(object): 16 """This class emulates an actual task. 17 18 It does not do the actual work, but simply returns the result as given when 19 this task is constructed. 20 """ 21 22 def __init__(self, stage, identifier, cost=0): 23 """Set up the results for this task. 24 25 Args: 26 stage: the stage of this test is in. 27 identifier: the identifier of this task. 28 cost: the mock cost of this task. 29 30 The _cost field stored the cost. Once this task is performed, i.e., by 31 calling the work method or by setting the result from other task, the 32 _cost field will have this cost. The stage field verifies that the module 33 being tested and the unitest are in the same stage. If the unitest does 34 not care about cost of this task, the cost parameter should be leaved 35 blank. 36 """ 37 38 self._identifier = identifier 39 self._cost = cost 40 self._stage = stage 41 42 # Indicate that this method has not been performed yet. 43 self._performed = False 44 45 def __eq__(self, other): 46 if isinstance(other, MockTask): 47 return self._identifier == other.GetIdentifier( 48 self._stage 49 ) and self._cost == other.GetResult(self._stage) 50 return False 51 52 def GetIdentifier(self, stage): 53 assert stage == self._stage 54 return self._identifier 55 56 def SetResult(self, stage, cost): 57 assert stage == self._stage 58 self._cost = cost 59 self._performed = True 60 61 def Work(self, stage): 62 assert stage == self._stage 63 self._performed = True 64 65 def GetResult(self, stage): 66 assert stage == self._stage 67 return self._cost 68 69 def Done(self, stage): 70 """Indicates whether the task has been performed.""" 71 72 assert stage == self._stage 73 return self._performed 74 75 def LogSteeringCost(self): 76 pass 77 78 79class IdentifierMockTask(MockTask): 80 """This class defines the mock task that does not consider the cost. 81 82 The task instances will be inserted into a set. Therefore the hash and the 83 equal methods are overridden. The unittests that compares identities of the 84 tasks for equality can use this mock task instead of the base mock tack. 85 """ 86 87 def __hash__(self): 88 return self._identifier 89 90 def __eq__(self, other): 91 if isinstance(other, MockTask): 92 return self._identifier == other.GetIdentifier(self._stage) 93 return False 94