Home | History | Annotate | Download | only in system
      1 # Copyright (C) 2010 Research in Motion Ltd. All rights reserved.
      2 #
      3 # Redistribution and use in source and binary forms, with or without
      4 # modification, are permitted provided that the following conditions are
      5 # met:
      6 #
      7 #    * Redistributions of source code must retain the above copyright
      8 # notice, this list of conditions and the following disclaimer.
      9 #    * Redistributions in binary form must reproduce the above
     10 # copyright notice, this list of conditions and the following disclaimer
     11 # in the documentation and/or other materials provided with the
     12 # distribution.
     13 #    * Neither the name of Research in Motion Ltd. nor the names of its
     14 # contributors may be used to endorse or promote products derived from
     15 # this software without specific prior written permission.
     16 #
     17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 
     29 import unittest
     30 
     31 from webkitpy.common.system.outputcapture import OutputCapture
     32 from webkitpy.common.system.user import User
     33 
     34 class UserTest(unittest.TestCase):
     35 
     36     example_user_response = "example user response"
     37 
     38     def test_prompt_repeat(self):
     39         self.repeatsRemaining = 2
     40         def mock_raw_input(message):
     41             self.repeatsRemaining -= 1
     42             if not self.repeatsRemaining:
     43                 return UserTest.example_user_response
     44             return None
     45         self.assertEqual(User.prompt("input", repeat=self.repeatsRemaining, raw_input=mock_raw_input), UserTest.example_user_response)
     46 
     47     def test_prompt_when_exceeded_repeats(self):
     48         self.repeatsRemaining = 2
     49         def mock_raw_input(message):
     50             self.repeatsRemaining -= 1
     51             return None
     52         self.assertEqual(User.prompt("input", repeat=self.repeatsRemaining, raw_input=mock_raw_input), None)
     53 
     54     def test_prompt_with_list(self):
     55         def run_prompt_test(inputs, expected_result, can_choose_multiple=False):
     56             def mock_raw_input(message):
     57                 return inputs.pop(0)
     58             output_capture = OutputCapture()
     59             actual_result = output_capture.assert_outputs(
     60                 self,
     61                 User.prompt_with_list,
     62                 args=["title", ["foo", "bar"]],
     63                 kwargs={"can_choose_multiple": can_choose_multiple, "raw_input": mock_raw_input},
     64                 expected_stdout="title\n 1. foo\n 2. bar\n")
     65             self.assertEqual(actual_result, expected_result)
     66             self.assertEqual(len(inputs), 0)
     67 
     68         run_prompt_test(["1"], "foo")
     69         run_prompt_test(["badinput", "2"], "bar")
     70 
     71         run_prompt_test(["1,2"], ["foo", "bar"], can_choose_multiple=True)
     72         run_prompt_test(["  1,  2   "], ["foo", "bar"], can_choose_multiple=True)
     73         run_prompt_test(["all"], ["foo", "bar"], can_choose_multiple=True)
     74         run_prompt_test([""], ["foo", "bar"], can_choose_multiple=True)
     75         run_prompt_test(["  "], ["foo", "bar"], can_choose_multiple=True)
     76         run_prompt_test(["badinput", "all"], ["foo", "bar"], can_choose_multiple=True)
     77 
     78     def test_confirm(self):
     79         test_cases = (
     80             (("Continue? [Y/n]: ", True), (User.DEFAULT_YES, 'y')),
     81             (("Continue? [Y/n]: ", False), (User.DEFAULT_YES, 'n')),
     82             (("Continue? [Y/n]: ", True), (User.DEFAULT_YES, '')),
     83             (("Continue? [Y/n]: ", False), (User.DEFAULT_YES, 'q')),
     84             (("Continue? [y/N]: ", True), (User.DEFAULT_NO, 'y')),
     85             (("Continue? [y/N]: ", False), (User.DEFAULT_NO, 'n')),
     86             (("Continue? [y/N]: ", False), (User.DEFAULT_NO, '')),
     87             (("Continue? [y/N]: ", False), (User.DEFAULT_NO, 'q')),
     88         )
     89         for test_case in test_cases:
     90             expected, inputs = test_case
     91 
     92             def mock_raw_input(message):
     93                 self.assertEquals(expected[0], message)
     94                 return inputs[1]
     95 
     96             result = User().confirm(default=inputs[0],
     97                                     raw_input=mock_raw_input)
     98             self.assertEquals(expected[1], result)
     99 
    100     def test_warn_if_application_is_xcode(self):
    101         output = OutputCapture()
    102         user = User()
    103         output.assert_outputs(self, user._warn_if_application_is_xcode, ["TextMate"])
    104         output.assert_outputs(self, user._warn_if_application_is_xcode, ["/Applications/TextMate.app"])
    105         output.assert_outputs(self, user._warn_if_application_is_xcode, ["XCode"])  # case sensitive matching
    106 
    107         xcode_warning = "Instead of using Xcode.app, consider using EDITOR=\"xed --wait\".\n"
    108         output.assert_outputs(self, user._warn_if_application_is_xcode, ["Xcode"], expected_stdout=xcode_warning)
    109         output.assert_outputs(self, user._warn_if_application_is_xcode, ["/Developer/Applications/Xcode.app"], expected_stdout=xcode_warning)
    110