From 6dd30af901a86a2200c93fd8ec36f668a3d1f2db Mon Sep 17 00:00:00 2001 From: Laurent Sansonetti Date: Mon, 8 Dec 2014 13:34:08 +0100 Subject: [PATCH] android support --- lib/motion/spec.rb | 60 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/lib/motion/spec.rb b/lib/motion/spec.rb index 11590888..26b16d00 100644 --- a/lib/motion/spec.rb +++ b/lib/motion/spec.rb @@ -335,7 +335,12 @@ module Bacon # If an exception occurred, we definitely don't need to schedule any more blocks unless @exception_occurred @postponed_blocks_count += 1 - performSelector("run_postponed_block:", withObject:block, afterDelay:seconds) + if defined?(NSObject) + performSelector("run_postponed_block:", withObject:block, afterDelay:seconds) + else + sleep seconds + run_postponed_block(block) + end end end @@ -347,7 +352,12 @@ module Bacon else @postponed_blocks_count += 1 @postponed_block = block - performSelector("postponed_block_timeout_exceeded", withObject:nil, afterDelay:timeout) + if defined?(NSObject) + performSelector("postponed_block_timeout_exceeded", withObject:nil, afterDelay:timeout) + else + sleep timeout + postponed_block_timeout_exceeded + end end end end @@ -362,7 +372,12 @@ module Bacon @postponed_block = block @observed_object_and_key_path = [object_to_observe, key_path] object_to_observe.addObserver(self, forKeyPath:key_path, options:0, context:nil) - performSelector("postponed_change_block_timeout_exceeded", withObject:nil, afterDelay:timeout) + if defined?(NSObject) + performSelector("postponed_change_block_timeout_exceeded", withObject:nil, afterDelay:timeout) + else + sleep timeout + postponed_change_block_timeout_exceeded + end end end end @@ -392,8 +407,10 @@ module Bacon end def resume - NSObject.cancelPreviousPerformRequestsWithTarget(self, selector:'postponed_block_timeout_exceeded', object:nil) - NSObject.cancelPreviousPerformRequestsWithTarget(self, selector:'postponed_change_block_timeout_exceeded', object:nil) + if defined?(NSObject) + NSObject.cancelPreviousPerformRequestsWithTarget(self, selector:'postponed_block_timeout_exceeded', object:nil) + NSObject.cancelPreviousPerformRequestsWithTarget(self, selector:'postponed_change_block_timeout_exceeded', object:nil) + end remove_observer! block, @postponed_block = @postponed_block, nil run_postponed_block(block) @@ -424,8 +441,10 @@ module Bacon end def cancel_scheduled_requests! - NSObject.cancelPreviousPerformRequestsWithTarget(@context) - NSObject.cancelPreviousPerformRequestsWithTarget(self) + if defined?(NSObject) + NSObject.cancelPreviousPerformRequestsWithTarget(@context) + NSObject.cancelPreviousPerformRequestsWithTarget(self) + end end def exit_spec @@ -449,8 +468,13 @@ module Bacon } ErrorLog << "\n" else - # Pure NSException. - ErrorLog << "#{e.name}: #{e.reason}\n" + if defined?(NSException) + # Pure NSException. + ErrorLog << "#{e.name}: #{e.reason}\n" + else + # Pure Java exception. + ErrorLog << "#{e.class.toString} : #{e.getMessage}" + end end @error = if e.kind_of? Error @@ -484,7 +508,11 @@ module Bacon @timer ||= Time.now Counter[:context_depth] += 1 handle_specification_begin(current_context.name) - current_context.performSelector("run", withObject:nil, afterDelay:0) + if defined?(NSObject) + current_context.performSelector("run", withObject:nil, afterDelay:0) + else + current_context.run + end end def self.context_did_finish(context) @@ -496,7 +524,11 @@ module Bacon else # DONE handle_summary - exit(Counter.values_at(:failed, :errors).inject(:+)) + if defined?(NSObject) + exit(Counter.values_at(:failed, :errors).inject(:+)) + else + # In Android there is no need to exit as we terminate the activity right after Bacon. + end end end @@ -520,7 +552,11 @@ module Bacon #return unless name =~ RestrictContext if spec = current_specification - spec.performSelector("run", withObject:nil, afterDelay:0) + if defined?(NSObject) + spec.performSelector("run", withObject:nil, afterDelay:0) + else + spec.run + end else Bacon.context_did_finish(self) end