1#!/usr/bin/env python 2# Copyright (c) 2012 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"""Unittest for android_xml.py.""" 7 8import os 9import StringIO 10import sys 11import unittest 12 13if __name__ == '__main__': 14 sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) 15 16from grit import util 17from grit.format import android_xml 18from grit.node import message 19from grit.tool import build 20 21 22class AndroidXmlUnittest(unittest.TestCase): 23 24 def testMessages(self): 25 root = util.ParseGrdForUnittest(ur""" 26 <messages> 27 <message name="IDS_SIMPLE" desc="A vanilla string"> 28 Martha 29 </message> 30 <message name="IDS_ONE_LINE" desc="On one line">sat and wondered</message> 31 <message name="IDS_QUOTES" desc="A string with quotation marks"> 32 out loud, "Why don't I build a flying car?" 33 </message> 34 <message name="IDS_MULTILINE" desc="A string split over several lines"> 35 She gathered 36wood, charcoal, and 37a sledge hammer. 38 </message> 39 <message name="IDS_WHITESPACE" desc="A string with extra whitespace."> 40 ''' How old fashioned -- she thought. ''' 41 </message> 42 <message name="IDS_PRODUCT_SPECIFIC_product_nosdcard" 43 desc="A string that only applies if there's no sdcard"> 44 Lasers will probably be helpful. 45 </message> 46 <message name="IDS_PRODUCT_DEFAULT" desc="New style product tag" 47 formatter_data="android_java_product=default android_java_name=custom_name"> 48 You have an SD card 49 </message> 50 <message name="IDS_PLACEHOLDERS" desc="A string with placeholders"> 51 I'll buy a <ph name="WAVELENGTH">%d<ex>200</ex></ph> nm laser at <ph name="STORE_NAME">%s<ex>the grocery store</ex></ph>. 52 </message> 53 </messages> 54 """) 55 56 buf = StringIO.StringIO() 57 build.RcBuilder.ProcessNode(root, DummyOutput('android', 'en'), buf) 58 output = buf.getvalue() 59 expected = ur""" 60<?xml version="1.0" encoding="utf-8"?> 61<resources xmlns:android="http://schemas.android.com/apk/res/android"> 62<string name="simple">"Martha"</string> 63<string name="one_line">"sat and wondered"</string> 64<string name="quotes">"out loud, \"Why don\'t I build a flying car?\""</string> 65<string name="multiline">"She gathered 66wood, charcoal, and 67a sledge hammer."</string> 68<string name="whitespace">" How old fashioned -- she thought. "</string> 69<string name="product_specific" product="nosdcard">"Lasers will probably be helpful."</string> 70<string name="custom_name" product="default">"You have an SD card"</string> 71<string name="placeholders">"I\'ll buy a %d nm laser at %s."</string> 72</resources> 73""" 74 self.assertEqual(output.strip(), expected.strip()) 75 76 def testTaggedOnly(self): 77 root = util.ParseGrdForUnittest(ur""" 78 <messages> 79 <message name="IDS_HELLO" desc="" formatter_data="android_java"> 80 Hello 81 </message> 82 <message name="IDS_WORLD" desc=""> 83 world 84 </message> 85 </messages> 86 """) 87 88 msg_hello, msg_world = root.GetChildrenOfType(message.MessageNode) 89 self.assertTrue(android_xml.ShouldOutputNode(msg_hello, tagged_only=True)) 90 self.assertFalse(android_xml.ShouldOutputNode(msg_world, tagged_only=True)) 91 self.assertTrue(android_xml.ShouldOutputNode(msg_hello, tagged_only=False)) 92 self.assertTrue(android_xml.ShouldOutputNode(msg_world, tagged_only=False)) 93 94 95class DummyOutput(object): 96 97 def __init__(self, type, language): 98 self.type = type 99 self.language = language 100 101 def GetType(self): 102 return self.type 103 104 def GetLanguage(self): 105 return self.language 106 107 def GetOutputFilename(self): 108 return 'hello.gif' 109 110if __name__ == '__main__': 111 unittest.main() 112