Skip to content

Instantly share code, notes, and snippets.

@wndxlori
Last active November 6, 2018 00:32
Show Gist options
  • Save wndxlori/6e685d0d67445df09b94d424b9993eed to your computer and use it in GitHub Desktop.
Save wndxlori/6e685d0d67445df09b94d424b9993eed to your computer and use it in GitHub Desktop.
Testing navigation bar buttons in RubyMotion

This is in answer to a question than came up in a course I am teaching. There do not appear to be any good examples of testing navigation bar button items, so I ran up this quick example. Two things of note:

  1. You need to explicitly call your controller's "on_load" or similar method that actually adds the UI items, in your before block if you actually want to test those items. In the case of the default Red Potion app, we create the nav bar buttons in HomeScreen.on_load, so we need to call that method in the before block of our spec, to ensure the nav buttons are present.

  2. The navigation bar is accessed from your UIViewController as navigationItem, and it is a UINavigationItem. You should check out the docs for these classes.

https://developer.apple.com/documentation/uikit/uiviewcontroller?language=objc https://developer.apple.com/documentation/uikit/uinavigationitem?language=objc

class HomeScreen < PM::Screen
title "Your title here"
stylesheet HomeScreenStylesheet
def on_load
set_nav_bar_button :left, system_item: :camera, action: :nav_left_button
set_nav_bar_button :right, title: "Right", action: :nav_right_button
@hello_world = append!(UILabel, :hello_world)
end
def nav_left_button
mp 'Left button'
end
def nav_right_button
mp 'Right button'
end
# You don't have to reapply styles to all UIViews, if you want to optimize, another way to do it
# is tag the views you need to restyle in your stylesheet, then only reapply the tagged views, like so:
# def logo(st)
# st.frame = {t: 10, w: 200, h: 96}
# st.centered = :horizontal
# st.image = image.resource('logo')
# st.tag(:reapply_style)
# end
#
# Then in will_animate_rotate
# find(:reapply_style).reapply_styles#
# Remove the following if you're only using portrait
def will_animate_rotate(orientation, duration)
find.all.reapply_styles
end
end
describe "HomeScreen" do
tests HomeScreen
before do
@controller = HomeScreen.new
@controller.on_load
end
it "should include the text label we added" do
view('Hello World').should.not.be.nil
end
it "should have right nav bar item" do
@controller.navigationItem.rightBarButtonItem.should.not.be.nil
end
it "should have left nav bar item" do
@controller.navigationItem.leftBarButtonItem.should.not.be.nil
end
it "should not have back nav bar item" do
@controller.navigationItem.backBarButtonItem.should.be.nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment