1# Copyright 2012 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4import optparse 5import os 6import unittest 7 8from telemetry.internal.browser import browser_options 9 10 11class BrowserOptionsTest(unittest.TestCase): 12 def testDefaults(self): 13 options = browser_options.BrowserFinderOptions() 14 parser = options.CreateParser() 15 parser.add_option('-x', action='store', default=3) 16 parser.parse_args(['--browser', 'any']) 17 self.assertEquals(options.x, 3) # pylint: disable=no-member 18 19 def testDefaultsPlusOverride(self): 20 options = browser_options.BrowserFinderOptions() 21 parser = options.CreateParser() 22 parser.add_option('-x', action='store', default=3) 23 parser.parse_args(['--browser', 'any', '-x', 10]) 24 self.assertEquals(options.x, 10) # pylint: disable=no-member 25 26 def testDefaultsDontClobberPresetValue(self): 27 options = browser_options.BrowserFinderOptions() 28 setattr(options, 'x', 7) 29 parser = options.CreateParser() 30 parser.add_option('-x', action='store', default=3) 31 parser.parse_args(['--browser', 'any']) 32 self.assertEquals(options.x, 7) # pylint: disable=no-member 33 34 def testCount0(self): 35 options = browser_options.BrowserFinderOptions() 36 parser = options.CreateParser() 37 parser.add_option('-x', action='count', dest='v') 38 parser.parse_args(['--browser', 'any']) 39 self.assertEquals(options.v, None) # pylint: disable=no-member 40 41 def testCount2(self): 42 options = browser_options.BrowserFinderOptions() 43 parser = options.CreateParser() 44 parser.add_option('-x', action='count', dest='v') 45 parser.parse_args(['--browser', 'any', '-xx']) 46 self.assertEquals(options.v, 2) # pylint: disable=no-member 47 48 def testOptparseMutabilityWhenSpecified(self): 49 options = browser_options.BrowserFinderOptions() 50 parser = options.CreateParser() 51 parser.add_option('-x', dest='verbosity', action='store_true') 52 options_ret, _ = parser.parse_args(['--browser', 'any', '-x']) 53 self.assertEquals(options_ret, options) 54 self.assertTrue(options.verbosity) 55 56 def testOptparseMutabilityWhenNotSpecified(self): 57 options = browser_options.BrowserFinderOptions() 58 59 parser = options.CreateParser() 60 parser.add_option('-x', dest='verbosity', action='store_true') 61 options_ret, _ = parser.parse_args(['--browser', 'any']) 62 self.assertEquals(options_ret, options) 63 self.assertFalse(options.verbosity) 64 65 def testProfileDirDefault(self): 66 options = browser_options.BrowserFinderOptions() 67 parser = options.CreateParser() 68 parser.parse_args(['--browser', 'any']) 69 self.assertEquals(options.browser_options.profile_dir, None) 70 71 def testProfileDir(self): 72 options = browser_options.BrowserFinderOptions() 73 parser = options.CreateParser() 74 # Need to use a directory that exists. 75 current_dir = os.path.dirname(__file__) 76 parser.parse_args(['--browser', 'any', '--profile-dir', current_dir]) 77 self.assertEquals(options.browser_options.profile_dir, current_dir) 78 79 def testExtraBrowserArgs(self): 80 options = browser_options.BrowserFinderOptions() 81 parser = options.CreateParser() 82 parser.parse_args(['--extra-browser-args=--foo --bar']) 83 84 self.assertEquals(options.browser_options.extra_browser_args, 85 set(['--foo', '--bar'])) 86 87 def testMergeDefaultValues(self): 88 options = browser_options.BrowserFinderOptions() 89 options.already_true = True 90 options.already_false = False 91 options.override_to_true = False 92 options.override_to_false = True 93 94 parser = optparse.OptionParser() 95 parser.add_option('--already_true', action='store_true') 96 parser.add_option('--already_false', action='store_true') 97 parser.add_option('--unset', action='store_true') 98 parser.add_option('--default_true', action='store_true', default=True) 99 parser.add_option('--default_false', action='store_true', default=False) 100 parser.add_option('--override_to_true', action='store_true', default=False) 101 parser.add_option('--override_to_false', action='store_true', default=True) 102 103 options.MergeDefaultValues(parser.get_default_values()) 104 105 self.assertTrue(options.already_true) 106 self.assertFalse(options.already_false) 107 self.assertTrue(options.unset is None) 108 self.assertTrue(options.default_true) 109 self.assertFalse(options.default_false) 110 self.assertFalse(options.override_to_true) 111 self.assertTrue(options.override_to_false) 112