180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru'''
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruCompares the rendererings of serialized SkPictures to expected images.
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruLaunch with --help to see more information.
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruCopyright 2012 Google Inc.
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruUse of this source code is governed by a BSD-style license that can be
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querufound in the LICENSE file.
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru'''
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru# common Python modules
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruimport os
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruimport optparse
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruimport sys
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruimport shutil
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruimport tempfile
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru# modules declared within this same directory
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruimport test_rendering
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruUSAGE_STRING = 'Usage: %s input... expectedDir'
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruHELP_STRING = '''
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruTakes input SkPicture files and renders them as PNG files, and then compares
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruthose resulting PNG files against PNG files found in expectedDir.
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruEach instance of "input" can be either a file (name must end in .skp), or a
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querudirectory (in which case this script will process all .skp files within the
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querudirectory).
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru'''
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querudef ModeParse(option, opt_str, value, parser):
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    """Parses the --mode option of the commandline.
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    The --mode option will either take in three parameters (if tile or
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    pow2tile) or a single parameter (otherwise).
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    """
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    result = [value]
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if value == "tile":
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru          if (len(parser.rargs) < 2):
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru              raise optparse.OptionValueError(("--mode tile mising width"
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                               " and/or height parameters"))
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru          result.extend(parser.rargs[:2])
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru          del parser.rargs[:2]
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    elif value == "pow2tile":
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru          if (len(parser.rargs) < 2):
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru              raise optparse.OptionValueError(("--mode pow2tile mising minWidth"
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                               " and/or height parameters"))
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru          result.extend(parser.rargs[:2])
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru          del parser.rargs[:2]
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    setattr(parser.values, option.dest, result)
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querudef Main(args):
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    """Allow other scripts to call this script with fake command-line args.
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    @param The commandline argument list
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    """
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    parser = optparse.OptionParser(USAGE_STRING % '%prog' + HELP_STRING)
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    parser.add_option('--render_dir', dest='render_dir',
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    help = ("specify the location to output the rendered files."
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                              " Default is a temp directory."))
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    parser.add_option('--diff_dir', dest='diff_dir',
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    help = ("specify the location to output the diff files."
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                              " Default is a temp directory."))
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    parser.add_option('--mode', dest='mode', type='string',
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                      action="callback", callback=ModeParse,
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                      help = ("specify how rendering is to be done."))
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    parser.add_option('--device', dest='device',
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                      help = ("specify the device to render to."))
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    options, arguments = parser.parse_args(args)
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (len(arguments) < 3):
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        print("Expected at least one input and one ouput folder.")
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        parser.print_help()
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        sys.exit(-1)
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    inputs = arguments[1:-1]
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    expected_dir = arguments[-1]
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    extra_args = ''
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (options.mode is not None):
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        extra_args += ' --mode %s' % ' '.join(options.mode)
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (options.device is not None):
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        extra_args += ' --device %s' % options.device
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    test_rendering.TestRenderSkps(inputs, expected_dir, options.render_dir,
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                  options.diff_dir, 'render_pictures',
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                  extra_args)
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruif __name__ == '__main__':
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    Main(sys.argv)
98