ruboto-core 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ require 'ruboto'
2
+
3
+ ruboto_import_widgets :ImageButton, :LinearLayout, :TextView
4
+
5
+ $activity.handle_create do |bundle|
6
+ setTitle 'This is the Title'
7
+
8
+ setup_content do
9
+ linear_layout :orientation => LinearLayout::VERTICAL do
10
+ @text_view = text_view :text => 'What hath Matz wrought?', :id => 42
11
+ image_button :image_resource => $package.R::drawable::icon, :width => :wrap_content, :id => 43
12
+ end
13
+ end
14
+
15
+ handle_click do |view|
16
+ if view.id == 43
17
+ @text_view.setText 'What hath Matz wrought!'
18
+ toast 'Flipped a bit via butterfly'
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,31 @@
1
+ require 'ruboto'
2
+
3
+ ruboto_import_widgets :Button, :ImageButton, :LinearLayout, :TextView
4
+
5
+ $activity.handle_create do |bundle|
6
+ setTitle 'This is the Title'
7
+
8
+ setup_content do
9
+ linear_layout :orientation => LinearLayout::VERTICAL do
10
+ @text_view = text_view :text => 'What hath Matz wrought?', :id => 42
11
+ button :text => 'Button', :width => :wrap_content, :id => 44
12
+ image_button :image_resource => $package.R::drawable::icon, :width => :wrap_content, :id => 43
13
+ end
14
+ end
15
+
16
+ handle_click do |view|
17
+ if view.id == 43
18
+ @text_view.text = 'Image button pressed'
19
+ elsif view.id == 44
20
+ @text_view.text = 'Button pressed'
21
+ end
22
+ end
23
+
24
+ # handle_image_click do |view|
25
+ # if view.id == 43
26
+ # @text_view.text = 'Image button pressed'
27
+ # elsif view.id == 44
28
+ # @text_view.text = 'Button pressed'
29
+ # end
30
+ # end
31
+ end
@@ -0,0 +1,27 @@
1
+ activity Java::org.ruboto.test_app.RubotoTestAppActivity
2
+
3
+ setup do |activity|
4
+ start = Time.now
5
+ loop do
6
+ @text_view = activity.find_view_by_id 42
7
+ break if @text_view || (Time.now - start > 60)
8
+ sleep 1
9
+ end
10
+ assert @text_view
11
+ end
12
+
13
+ test('initial setup') do |activity|
14
+ assert_equal "What hath Matz wrought?", @text_view.text
15
+ end
16
+
17
+ test('button changes text') do |activity|
18
+ button = activity.find_view_by_id 44
19
+ button.perform_click
20
+ assert_equal 'Button pressed', @text_view.text
21
+ end
22
+
23
+ test('image button changes text') do |activity|
24
+ image_button = activity.find_view_by_id 43
25
+ image_button.perform_click
26
+ assert_equal 'Image button pressed', @text_view.text
27
+ end
@@ -0,0 +1,21 @@
1
+ activity Java::org.ruboto.test_app.RubotoTestAppActivity
2
+
3
+ setup do |activity|
4
+ start = Time.now
5
+ loop do
6
+ @text_view = activity.findViewById(42)
7
+ break if @text_view || (Time.now - start > 60)
8
+ sleep 1
9
+ end
10
+ assert @text_view
11
+ end
12
+
13
+ test('initial setup') do |activity|
14
+ assert_equal "What hath Matz wrought?", @text_view.text
15
+ end
16
+
17
+ test('button changes text') do |activity|
18
+ button = activity.findViewById(43)
19
+ button.performClick
20
+ assert_equal "What hath Matz wrought!", @text_view.text
21
+ end
@@ -0,0 +1,54 @@
1
+ require File.expand_path("test_helper", File.dirname(__FILE__))
2
+
3
+ module AppTestMethods
4
+ include RubotoTest
5
+
6
+ if ['android-7', 'android-8'].include? ANDROID_OS
7
+ def test_nothing
8
+ puts "Skipping instrumentation tests on #{ANDROID_OS} since they don't work."
9
+ end
10
+ else
11
+ def test_that_tests_work_on_new_project
12
+ run_app_tests
13
+ end
14
+
15
+ if not ON_JRUBY_JARS_1_5_6
16
+ def test_that_yaml_loads
17
+ assert_code "with_large_stack{require 'yaml'}"
18
+ end
19
+ else
20
+ puts "Skipping YAML tests on jruby-jars-1.5.6"
21
+ end
22
+
23
+ def test_file_read_source_file
24
+ assert_code "File.read(__FILE__)"
25
+ end
26
+
27
+ Dir.chdir File.expand_path('activity', File.dirname(__FILE__)) do
28
+ Dir['*_test.rb'].each do |test|
29
+ class_eval %Q{
30
+ def test_#{test.chomp('_test.rb')}
31
+ filename = "#{APP_DIR}/assets/scripts/ruboto_test_app_activity.rb"
32
+ test_filename = "#{APP_DIR}/test/assets/scripts/ruboto_test_app_activity_test.rb"
33
+ File.open(filename, 'w') { |f| f << File.read('#{PROJECT_DIR}/test/activity/#{test.gsub('_test', '')}') }
34
+ File.open(test_filename, 'w') { |f| f << File.read('#{PROJECT_DIR}/test/activity/#{test}') }
35
+ run_app_tests
36
+ end
37
+ }
38
+ puts "Creating test from file #{PROJECT_DIR}/test/activity/#{test}"
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def assert_code(code)
45
+ filename = "#{APP_DIR}/assets/scripts/ruboto_test_app_activity.rb"
46
+ s = File.read(filename)
47
+ s.gsub!(/(require 'ruboto')/, "\\1\n#{code}")
48
+ File.open(filename, 'w') { |f| f << s }
49
+ run_app_tests
50
+ end
51
+
52
+ end
53
+
54
+ end
data/test/rake_test.rb ADDED
@@ -0,0 +1,53 @@
1
+ require File.expand_path("test_helper", File.dirname(__FILE__))
2
+ require 'fileutils'
3
+
4
+ $stdout.sync = true
5
+
6
+ class RakeTest < Test::Unit::TestCase
7
+ def setup
8
+ generate_app
9
+ end
10
+
11
+ def teardown
12
+ cleanup_app
13
+ end
14
+
15
+ def test_that_update_scripts_task_copies_files_to_sdcard_if_permissions_are_set
16
+ manifest = File.read("#{APP_DIR}/AndroidManifest.xml")
17
+ manifest.gsub! %r{</manifest>}, %Q{ <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />\n</manifest>}
18
+ File.open("#{APP_DIR}/AndroidManifest.xml", 'w') { |f| f << manifest }
19
+
20
+ Dir.chdir APP_DIR do
21
+ system 'rake install:restart:clean'
22
+ assert_equal 0, $?
23
+ end
24
+
25
+ # wait_for_dir("/mnt/sdcard/Android/data/#{PACKAGE}/files/scripts")
26
+ wait_for_dir("/sdcard/Android/data/#{PACKAGE}/files/scripts")
27
+ end
28
+
29
+ if ANDROID_OS == 'android-7'
30
+ puts "Skipping sdcard test since files on sdcard are not removed on android-7 on app uninstall"
31
+ else
32
+ def test_that_update_scripts_task_copies_files_to_app_directory_when_permissions_are_not_set
33
+ Dir.chdir APP_DIR do
34
+ system 'rake install:restart:clean'
35
+ assert_equal 0, $?
36
+ end
37
+ wait_for_dir("/data/data/#{PACKAGE}/files/scripts")
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def wait_for_dir(dir)
44
+ puts "Waiting for app to generate script directory: #{dir}"
45
+ start = Time.now
46
+ loop do
47
+ break if `adb shell ls -d #{dir}`.chomp =~ %r{^#{dir}$}
48
+ flunk 'Timeout waiting for scripts directory to appear' if Time.now > start + 60
49
+ sleep 1
50
+ end
51
+ end
52
+
53
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path("test_helper", File.dirname(__FILE__))
2
+ require 'fileutils'
3
+ require 'test/app_test_methods'
4
+
5
+ class RubotoGenTest < Test::Unit::TestCase
6
+ include AppTestMethods
7
+
8
+ def setup
9
+ generate_app
10
+ end
11
+
12
+ def teardown
13
+ cleanup_app
14
+ end
15
+ end
16
+
17
+ if not RubotoTest::ON_JRUBY_JARS_1_5_6
18
+ class RubotoGenWithPsychTest < Test::Unit::TestCase
19
+ include AppTestMethods
20
+
21
+ def setup
22
+ generate_app :with_psych => true
23
+ end
24
+
25
+ def teardown
26
+ cleanup_app
27
+ end
28
+
29
+ def test_psych_jar_exists
30
+ assert File.exists?("#{APP_DIR}/libs/psych.jar"), "Failed to generate psych jar"
31
+ end
32
+
33
+ end
34
+ else
35
+ puts "Skipping Psych tests on jruby-jars-1.5.6"
36
+ end
@@ -0,0 +1,61 @@
1
+ require File.expand_path("test_helper", File.dirname(__FILE__))
2
+ require 'fileutils'
3
+ require 'test/app_test_methods'
4
+
5
+ module UpdateTestMethods
6
+ include RubotoTest
7
+
8
+ def setup(with_psych = false)
9
+ Dir.mkdir TMP_DIR unless File.exists? TMP_DIR
10
+ FileUtils.rm_rf APP_DIR if File.exists? APP_DIR
11
+ Dir.chdir TMP_DIR do
12
+ system "tar xzf #{PROJECT_DIR}/examples/RubotoTestApp_0.1.0_jruby_1.6.3.dev.tgz"
13
+ end
14
+ if ENV['ANDROID_HOME']
15
+ android_home = ENV['ANDROID_HOME']
16
+ else
17
+ android_home = File.dirname(File.dirname(`which adb`))
18
+ end
19
+ File.open("#{APP_DIR}/local.properties", 'w'){|f| f.puts "sdk.dir=#{android_home}"}
20
+ File.open("#{APP_DIR}/test/local.properties", 'w'){|f| f.puts "sdk.dir=#{android_home}"}
21
+ Dir.chdir APP_DIR do
22
+ FileUtils.touch "libs/psych.jar" if with_psych
23
+ system "#{RUBOTO_CMD} update app"
24
+ assert_equal 0, $?, "update app failed with return code #$?"
25
+ end
26
+ end
27
+
28
+ def teardown
29
+ cleanup_app
30
+ end
31
+
32
+ def test_properties_and_ant_file_has_no_duplicates
33
+ Dir.chdir APP_DIR do
34
+ assert File.readlines('test/build.properties').grep(/\w/).uniq!.nil?, 'Duplicate lines in build.properties'
35
+ assert_equal 1, File.readlines('test/build.xml').grep(/<macrodef name="run-tests-helper">/).size, 'Duplicate macro in build.xml'
36
+ end
37
+ end
38
+ end
39
+
40
+ class RubotoUpdateTest < Test::Unit::TestCase
41
+ include UpdateTestMethods
42
+ include AppTestMethods
43
+ end
44
+
45
+ if not RubotoTest::ON_JRUBY_JARS_1_5_6
46
+ class RubotoUpdateWithPsychTest < Test::Unit::TestCase
47
+ include UpdateTestMethods
48
+ include AppTestMethods
49
+
50
+ def setup
51
+ super(true)
52
+ end
53
+
54
+ def test_psych_jar_exists
55
+ assert File.exists?("#{APP_DIR}/libs/psych.jar"), "Failed to generate psych jar"
56
+ end
57
+
58
+ end
59
+ else
60
+ puts "Skipping Psych tests on jruby-jars-1.5.6"
61
+ end
@@ -0,0 +1,49 @@
1
+ require File.expand_path("test_helper", File.dirname(__FILE__))
2
+ require 'fileutils'
3
+
4
+ class ServiceTest < Test::Unit::TestCase
5
+ include RubotoTest
6
+
7
+ def setup
8
+ generate_app
9
+ end
10
+
11
+ def teardown
12
+ cleanup_app
13
+ end
14
+
15
+ def test_service_startup
16
+ Dir.chdir APP_DIR do
17
+ system "#{RUBOTO_CMD} gen class Service --name RubotoTestService"
18
+ service_filename = "#{APP_DIR}/assets/scripts/ruboto_test_service.rb"
19
+ assert File.exists? service_filename
20
+ File.open(service_filename, 'w'){|f| f << <<EOF}
21
+ require 'ruboto'
22
+
23
+ $service.handle_create do
24
+ Thread.start do
25
+ loop do
26
+ sleep 1
27
+ puts "\#{self.class} running..."
28
+ end
29
+ end
30
+ puts "\#{self.class} started."
31
+ android.app.Service::START_STICKY
32
+ end
33
+
34
+ $service.handle_start_command do
35
+ android.app.Service::START_STICKY
36
+ end
37
+ EOF
38
+
39
+ activity_filename = "#{APP_DIR}/assets/scripts/ruboto_test_app_activity.rb"
40
+ s = File.read(activity_filename)
41
+ s.gsub!(/^(end)$/, "
42
+ startService(android.content.Intent.new($activity.application_context, $package.RubotoTestService.java_class))
43
+ \\1\n")
44
+ File.open(activity_filename, 'w') { |f| f << s }
45
+ end
46
+ run_app_tests
47
+ end
48
+
49
+ end
data/test/test_helper.rb CHANGED
@@ -1,4 +1,114 @@
1
- require "test/unit"
1
+ require 'test/unit'
2
+ require 'rubygems'
2
3
 
3
- PROJECT_DIR = File.expand_path('..', File.dirname(__FILE__))
4
- $LOAD_PATH << PROJECT_DIR
4
+ module RubotoTest
5
+ PROJECT_DIR = File.expand_path('..', File.dirname(__FILE__))
6
+ $LOAD_PATH << PROJECT_DIR
7
+
8
+ gem_spec = Gem.searcher.find('jruby-jars')
9
+ raise StandardError.new("Can't find Gem specification jruby-jars.") unless gem_spec
10
+ JRUBY_JARS_VERSION = gem_spec.version
11
+ ON_JRUBY_JARS_1_5_6 = JRUBY_JARS_VERSION == Gem::Version.new('1.5.6')
12
+
13
+ PACKAGE = 'org.ruboto.test_app'
14
+ APP_NAME = 'RubotoTestApp'
15
+ TMP_DIR = File.join PROJECT_DIR, 'tmp'
16
+ APP_DIR = File.join TMP_DIR, APP_NAME
17
+ ANDROID_TARGET = ENV['ANDROID_TARGET'] || 'android-7'
18
+
19
+ VERSION_TO_API_LEVEL = {
20
+ '2.1' => 'android-7', '2.1-update1' => 'android-7', '2.2' => 'android-8',
21
+ '2.3' => 'android-9', '2.3.1' => 'android-9', '2.3.2' => 'android-9',
22
+ '2.3.3' => 'android-10', '2.3.4' => 'android-10',
23
+ '3.0' => 'android-11', '3.1' => 'android-12', '3.2' => 'android-13'
24
+ }
25
+
26
+ def self.version_from_device
27
+ puts "Reading OS version from device/emulator"
28
+ system "adb wait-for-device"
29
+ start = Time.now
30
+ IO.popen('adb bugreport').each_line do |line|
31
+ if line =~ /sdk-eng (.*?) .*? .*? test-keys/
32
+ version = $1
33
+ api_level = VERSION_TO_API_LEVEL[version]
34
+ raise "Unknown version: #{version}" if api_level.nil?
35
+ puts "Getting version from device/emulator took #{(Time.now - start).to_i}s"
36
+ return api_level
37
+ end
38
+ end
39
+ raise "Unable to read device/emulator apilevel"
40
+ end
41
+
42
+ ANDROID_OS = ENV['ANDROID_OS'] || version_from_device
43
+ RUBOTO_CMD = "ruby -rubygems -I #{PROJECT_DIR}/lib #{PROJECT_DIR}/bin/ruboto"
44
+
45
+ puts "ANDROID_OS: #{ANDROID_OS}"
46
+ end
47
+
48
+ class Test::Unit::TestCase
49
+ include RubotoTest
50
+ alias old_run run
51
+
52
+ def run(*args, &block)
53
+ mark_test_start("#{self.class.name}\##{method_name}")
54
+ old_run(*args, &block)
55
+ mark_test_end("#{self.class.name}\##{method_name}")
56
+ end
57
+
58
+ def mark_test_start(test_name)
59
+ @start_time = Time.now
60
+ log
61
+ log '=' * 80
62
+ log "Starting test #{test_name} at #{@start_time.strftime('%Y-%m-%d %H:%M:%S')}:"
63
+ log
64
+ end
65
+
66
+ def mark_test_end(test_name)
67
+ log
68
+ log "Ended test #{test_name}: #{passed? ? 'PASSED' : 'FAILED'} after #{(Time.now - @start_time).to_i}s"
69
+ log '=' * 80
70
+ log
71
+ end
72
+
73
+ def log(message = '')
74
+ puts message
75
+ system "adb shell log -t 'RUBOTO TEST' '#{message}'"
76
+ end
77
+
78
+ def generate_app(options = {})
79
+ with_psych = options.delete(:with_psych) || false
80
+ raise "Unknown options: #{options.inspect}" unless options.empty?
81
+ Dir.mkdir TMP_DIR unless File.exists? TMP_DIR
82
+ FileUtils.rm_rf APP_DIR if File.exists? APP_DIR
83
+ template_dir = "#{APP_DIR}_template_#{$$}#{'_with_psych' if with_psych}"
84
+ if File.exists?(template_dir)
85
+ puts "Copying app from template #{template_dir}"
86
+ FileUtils.cp_r template_dir, APP_DIR, :preserve => true
87
+ else
88
+ puts "Generating app #{APP_DIR}"
89
+ system "#{RUBOTO_CMD} gen app --package #{PACKAGE} --path #{APP_DIR} --name #{APP_NAME} --min_sdk #{ANDROID_TARGET} #{'--with-psych' if with_psych}"
90
+ if $? != 0
91
+ FileUtils.rm_rf template_dir
92
+ raise "gen app failed with return code #$?"
93
+ end
94
+ Dir.chdir APP_DIR do
95
+ system 'rake debug'
96
+ assert_equal 0, $?
97
+ end
98
+ puts "Storing app as template #{template_dir}"
99
+ FileUtils.cp_r APP_DIR, template_dir, :preserve => true
100
+ end
101
+ end
102
+
103
+ def cleanup_app
104
+ # FileUtils.rm_rf APP_DIR if File.exists? APP_DIR
105
+ end
106
+
107
+ def run_app_tests
108
+ Dir.chdir "#{APP_DIR}/test" do
109
+ system 'rake test:quick'
110
+ assert_equal 0, $?, "tests failed with return code #$?"
111
+ end
112
+ end
113
+
114
+ end