1 #!/usr/bin/env python 2 # Copyright 2013 The Chromium Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style license that can be 4 # found in the LICENSE file. 5 6 from copy import deepcopy 7 import json 8 import unittest 9 10 from future import Future 11 import manifest_data_source 12 from object_store_creator import ObjectStoreCreator 13 14 15 convert_and_annotate_docs = { 16 'name': { 17 'example': "My {{platform}}", 18 'name': 'name', 19 'level': 'required' 20 }, 21 'doc2': { 22 'level': 'required', 23 'name': 'doc2' 24 }, 25 'doc1': { 26 'level': 'required', 27 'name': 'doc1', 28 'children': { 29 'sub1': { 30 'annotations': ['not so important'], 31 'level': 'optional', 32 'name': 'sub1' 33 }, 34 'sub2': { 35 'level': 'required', 36 'name': 'sub2' 37 } 38 } 39 }, 40 'doc3': { 41 'level': 'only_one', 42 'name': 'doc3' 43 }, 44 'doc4': { 45 'level': 'recommended', 46 'name': 'doc4' 47 }, 48 'doc5': { 49 'level': 'only_one', 50 'name': 'doc5' 51 }, 52 'doc6': { 53 'level': 'optional', 54 'name': 'doc6' 55 } 56 } 57 58 59 class ManifestDataSourceTest(unittest.TestCase): 60 def testListifyAndSortDocs(self): 61 expected_docs = [ 62 { 63 'level': 'required', 64 'name': 'doc1', 65 'children': [ 66 { 67 'level': 'required', 68 'name': 'sub2' 69 }, 70 { 71 'annotations': ['not so important'], 72 'level': 'optional', 73 'name': 'sub1' 74 } 75 ] 76 }, 77 { 78 'level': 'required', 79 'name': 'doc2' 80 }, 81 { 82 'level': 'required', 83 'example': '"My App"', 84 'has_example': True, 85 'name': 'name' 86 }, 87 { 88 'level': 'recommended', 89 'name': 'doc4' 90 }, 91 { 92 'level': 'only_one', 93 'name': 'doc3' 94 }, 95 { 96 'level': 'only_one', 97 'name': 'doc5' 98 }, 99 { 100 'level': 'optional', 101 'name': 'doc6' 102 } 103 ] 104 105 self.assertEqual(expected_docs, manifest_data_source._ListifyAndSortDocs( 106 deepcopy(convert_and_annotate_docs), 'App')) 107 108 def testAnnotate(self): 109 expected_docs = [ 110 { 111 'level': 'required', 112 'name': 'doc1', 113 'children': [ 114 { 115 'level': 'required', 116 'name': 'sub2' 117 }, 118 { 119 'annotations': ['Optional', 'not so important'], 120 'level': 'optional', 121 'name': 'sub1', 122 'is_last': True 123 } 124 ] 125 }, 126 { 127 'level': 'required', 128 'name': 'doc2' 129 }, 130 { 131 'name': 'name', 132 'level': 'required', 133 'example': '"My App"', 134 'has_example': True 135 }, 136 { 137 'annotations': ['Recommended'], 138 'level': 'recommended', 139 'name': 'doc4' 140 }, 141 { 142 'annotations': ['Pick one (or none)'], 143 'level': 'only_one', 144 'name': 'doc3' 145 }, 146 { 147 'level': 'only_one', 148 'name': 'doc5' 149 }, 150 { 151 'annotations': ['Optional'], 152 'level': 'optional', 153 'name': 'doc6', 154 'is_last': True 155 } 156 ] 157 158 annotated = manifest_data_source._ListifyAndSortDocs( 159 deepcopy(convert_and_annotate_docs), 'App') 160 manifest_data_source._AddLevelAnnotations(annotated) 161 self.assertEqual(expected_docs, annotated) 162 163 def testExpandedExamples(self): 164 docs = { 165 'doc1': { 166 'name': 'doc1', 167 'example': { 168 'big': { 169 'nested': { 170 'json_example': ['with', 'more', 'json'] 171 } 172 } 173 } 174 } 175 } 176 177 expected_docs = [ 178 { 179 'name': 'doc1', 180 'children': [ 181 { 182 'name': 'big', 183 'children': [ 184 { 185 'name': 'nested', 186 'children': [ 187 { 188 'name': 'json_example', 189 'example': json.dumps(['with', 'more', 'json']), 190 'has_example': True 191 } 192 ] 193 } 194 ] 195 } 196 ] 197 } 198 ] 199 200 self.assertEqual( 201 expected_docs, manifest_data_source._ListifyAndSortDocs(docs, 'apps')) 202 203 def testNonExpandedExamples(self): 204 docs = { 205 'doc1': { 206 'name': 'doc1', 207 'example': {} 208 }, 209 'doc2': { 210 'name': 'doc2', 211 'example': [] 212 }, 213 'doc3': { 214 'name': 'doc3', 215 'example': [{}] 216 } 217 } 218 219 expected_docs = [ 220 { 221 'name': 'doc1', 222 'has_example': True, 223 'example': '{...}' 224 }, 225 { 226 'name': 'doc2', 227 'has_example': True, 228 'example': '[...]' 229 }, 230 { 231 'name': 'doc3', 232 'has_example': True, 233 'example': '[{...}]' 234 } 235 ] 236 self.assertEqual( 237 expected_docs, manifest_data_source._ListifyAndSortDocs(docs, 'apps')) 238 239 def testManifestDataSource(self): 240 manifest_features = { 241 'doc1': { 242 'name': 'doc1', 243 'platforms': ['apps', 'extensions'], 244 'example': {}, 245 'level': 'required' 246 }, 247 'doc1.sub1': { 248 'name': 'doc1.sub1', 249 'platforms': ['apps'], 250 'annotations': ['important!'], 251 'level': 'recommended' 252 } 253 } 254 255 expected_app = [ 256 { 257 'example': '{...}', 258 'has_example': True, 259 'level': 'required', 260 'name': 'doc1', 261 'platforms': ['apps', 'extensions'], 262 'children': [ 263 { 264 'annotations': [ 265 'Recommended', 266 'important!' 267 ], 268 'level': 'recommended', 269 'name': 'sub1', 270 'platforms': ['apps'], 271 'is_last': True 272 } 273 ], 274 'is_last': True 275 } 276 ] 277 278 class FakePlatformBundle(object): 279 def GetFeaturesBundle(self, platform): 280 return FakeFeaturesBundle() 281 282 class FakeFeaturesBundle(object): 283 def GetManifestFeatures(self): 284 return Future(value=manifest_features) 285 286 class FakeServerInstance(object): 287 def __init__(self): 288 self.platform_bundle = FakePlatformBundle() 289 self.object_store_creator = ObjectStoreCreator.ForTest() 290 291 mds = manifest_data_source.ManifestDataSource(FakeServerInstance(), None) 292 self.assertEqual(expected_app, mds.get('apps')) 293 294 if __name__ == '__main__': 295 unittest.main() 296