1# -*- coding: utf-8 -*- 2"""\ 3XBM related tests. 4""" 5from __future__ import unicode_literals, absolute_import 6from nose.tools import eq_, raises 7import nose 8import os 9import pyqrcode 10 11#Create by: 12# First I ran: pyqrcode.create('Test', scale=1).png('test.png') 13# Next, I used GIMP to convert it to a XBM file. 14expected = '''#define im_width 29 15#define im_height 29 16static unsigned char im_bits[] = { 17 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 18 0x00, 0x00, 0x00, 0x00, 0xf0, 0x17, 0xfd, 0x01, 0x10, 0xe4, 0x05, 0x01, 19 0xd0, 0xb5, 0x75, 0x01, 0xd0, 0xc5, 0x74, 0x01, 0xd0, 0xb5, 0x74, 0x01, 20 0x10, 0x24, 0x05, 0x01, 0xf0, 0x57, 0xfd, 0x01, 0x00, 0x70, 0x01, 0x00, 21 0x00, 0x06, 0x55, 0x01, 0xf0, 0xf0, 0x76, 0x00, 0xc0, 0xb5, 0xf3, 0x00, 22 0x80, 0x82, 0x70, 0x00, 0x40, 0xd4, 0x06, 0x01, 0x00, 0x50, 0xcc, 0x00, 23 0xf0, 0x47, 0xac, 0x00, 0x10, 0xb4, 0x7b, 0x01, 0xd0, 0x45, 0xaa, 0x00, 24 0xd0, 0xe5, 0x66, 0x00, 0xd0, 0x25, 0xe3, 0x01, 0x10, 0x64, 0x57, 0x00, 25 0xf0, 0xa7, 0xd5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 26 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };''' 27 28def decompose_xbm(s): 29 import re 30 31 width = re.search('width ([0-9]+)', s).group(1) 32 height = re.search('height ([0-9]+)', s).group(1) 33 bits = re.findall(r'(0x[0-9][0-9])', s) 34 35 return width, height, bits 36 37def test_xbm(): 38 """Test the xbm render against a known good. 39 40 This test checks the *values* contained in the XBM, not the text. 41 """ 42 c = pyqrcode.create('Test').xbm(scale=1) 43 44 #Testing number-by-number to get more useful failure message 45 c_width, c_height, c_bits = decompose_xbm(c) 46 e_width, e_height, e_bits = decompose_xbm(expected) 47 48 #Check the there is the same width and height 49 eq_(c_width, e_width) 50 eq_(c_height, e_height) 51 52 #Check that there is the same number of bits 53 eq_(len(c_bits), len(e_bits)) 54 55 #Check the bit values 56 for i in range(len(e_bits)): 57 eq_(c_bits[i], e_bits[i], 58 "Wrong value at {0}: {1} != {2}".format(i, c_bits[i], e_bits[i])) 59 60def test_xbm_with_tkinter(): 61 """Test XBM renderer is compatible with Tkinter 62 """ 63 #Under TOX tkinter testing does not work, skip if tox environemnt 64 if not os.getenv('DISPLAY'): 65 raise nose.SkipTest() 66 67 #Python 2 vs 3 68 try: 69 import Tkinter as tkinter 70 except: 71 import tkinter 72 73 code = pyqrcode.create('Test') 74 code_size = code.get_png_size(scale=1) 75 code_xbm = code.xbm(scale=1) 76 77 top = tkinter.Tk() 78 bitmap = tkinter.BitmapImage(data=code_xbm) 79 80 eq_(bitmap.width(), code_size) 81 eq_(bitmap.height(), code_size) 82 83if __name__ == '__main__': 84 import nose 85 nose.core.runmodule() 86