honeydew 0.12.0 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/.rspec +1 -1
  2. data/LICENSE.txt +1 -1
  3. data/README.md +79 -8
  4. data/Rakefile +2 -2
  5. data/examples/cucumber/Gemfile +5 -0
  6. data/{example → examples/cucumber}/Rakefile +0 -0
  7. data/{example → examples/cucumber}/features/example.feature +0 -0
  8. data/{example → examples/cucumber}/features/step_definitions/honeydew_steps.rb +0 -0
  9. data/examples/rspec/spec/browse_spec.rb +16 -0
  10. data/examples/rspec/spec/spec_helper.rb +6 -0
  11. data/honeydew.gemspec +17 -17
  12. data/honeydew.iml +12 -18
  13. data/lib/honeydew.rb +1 -72
  14. data/lib/honeydew/device.rb +79 -44
  15. data/lib/honeydew/device_actions.rb +32 -100
  16. data/lib/honeydew/device_commands.rb +36 -23
  17. data/lib/honeydew/device_matchers.rb +29 -22
  18. data/lib/honeydew/dsl.rb +1 -1
  19. data/lib/honeydew/honeydew.rb +82 -0
  20. data/lib/honeydew/version.rb +1 -1
  21. data/{android-server → server}/.gitignore +0 -0
  22. data/{android-server → server}/pom.xml +16 -6
  23. data/{android-server → server}/repo/com/google/android/uiautomator/4.1.1.4/_maven.repositories +0 -0
  24. data/{android-server → server}/repo/com/google/android/uiautomator/4.1.1.4/uiautomator-4.1.1.4.jar +0 -0
  25. data/{android-server → server}/repo/com/google/android/uiautomator/4.1.1.4/uiautomator-4.1.1.4.pom +0 -0
  26. data/{android-server → server}/repo/com/jayway/maven/plugins/android/generation2/android-maven-plugin/3.5.2-SNAPSHOT/android-maven-plugin-3.5.2-SNAPSHOT.jar +0 -0
  27. data/{android-server → server}/repo/com/jayway/maven/plugins/android/generation2/android-maven-plugin/3.5.2-SNAPSHOT/android-maven-plugin-3.5.2-SNAPSHOT.pom +0 -0
  28. data/{android-server → server}/repo/com/jayway/maven/plugins/android/generation2/android-maven-plugin/3.5.2-SNAPSHOT/maven-metadata-local.xml +0 -0
  29. data/{android-server → server}/repo/commons-jxpath/commons-jxpath/1.4-SNAPSHOT/_maven.repositories +0 -0
  30. data/{android-server → server}/repo/commons-jxpath/commons-jxpath/1.4-SNAPSHOT/commons-jxpath-1.4-SNAPSHOT.jar +0 -0
  31. data/{android-server → server}/repo/commons-jxpath/commons-jxpath/1.4-SNAPSHOT/commons-jxpath-1.4-SNAPSHOT.pom +0 -0
  32. data/{android-server → server}/repo/commons-jxpath/commons-jxpath/1.4-SNAPSHOT/maven-metadata-local.xml +0 -0
  33. data/{android-server → server}/repo/commons-jxpath/commons-jxpath/maven-metadata-local.xml +0 -0
  34. data/server/repo/fi/iki/elonen/nanohttpd-webserver/2.0.3/nanohttpd-webserver-2.0.3.jar +0 -0
  35. data/server/repo/fi/iki/elonen/nanohttpd-webserver/2.0.3/nanohttpd-webserver-2.0.3.pom +9 -0
  36. data/server/repo/fi/iki/elonen/nanohttpd-webserver/maven-metadata-local.xml +12 -0
  37. data/server/repo/fi/iki/elonen/nanohttpd/2.0.3/nanohttpd-2.0.3.jar +0 -0
  38. data/server/repo/fi/iki/elonen/nanohttpd/2.0.3/nanohttpd-2.0.3.pom +9 -0
  39. data/server/repo/fi/iki/elonen/nanohttpd/maven-metadata-local.xml +12 -0
  40. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/Action.java +13 -6
  41. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/ActionsExecutor.java +3 -2
  42. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/Command.java +6 -1
  43. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/Result.java +7 -7
  44. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/TestRunner.java +14 -6
  45. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/Click.java +3 -3
  46. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/ClickAndWaitForNewWindow.java +3 -3
  47. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/DumpWindowHierarchy.java +5 -5
  48. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/HasSettingsMenuItem.java +4 -5
  49. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/InspectOptionInSettingsMenu.java +3 -3
  50. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/IsButtonPresent.java +3 -3
  51. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/IsChildCountEqualTo.java +4 -4
  52. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/IsElementWithNestedTextPresent.java +3 -3
  53. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/IsOptionInSettingsMenuDisabled.java +1 -1
  54. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/IsOptionInSettingsMenuEnabled.java +1 -1
  55. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/IsTextPresent.java +3 -3
  56. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/LaunchApp.java +3 -3
  57. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/LaunchHome.java +3 -3
  58. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/LongClick.java +3 -3
  59. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/PressBack.java +3 -3
  60. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/PressEnter.java +4 -4
  61. data/server/src/main/java/com/amplify/honeydew_server/actions/ScrollToTextByIndex.java +27 -0
  62. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/SelectFromAppsList.java +4 -4
  63. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/SelectMenuInSettings.java +3 -3
  64. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/SetText.java +3 -3
  65. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/SetTextByIndex.java +2 -2
  66. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/SetTextByLabel.java +3 -3
  67. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/Unlock.java +3 -3
  68. data/{android-server/src/main/java/com/uiautomator_cucumber/android_server → server/src/main/java/com/amplify/honeydew_server}/actions/WakeUp.java +4 -4
  69. data/server/src/main/java/com/amplify/honeydew_server/httpd/RemoteCommandReceiver.java +66 -0
  70. data/spec/honeydew/device_matchers_spec.rb +21 -42
  71. data/spec/honeydew/device_spec.rb +38 -36
  72. data/spec/spec_helper.rb +2 -25
  73. metadata +93 -92
  74. data/android-server/src/main/java/com/uiautomator_cucumber/android_server/httpd/NanoHTTPD.java +0 -1100
  75. data/android-server/src/main/java/com/uiautomator_cucumber/android_server/httpd/RemoteCommandReceiver.java +0 -64
  76. data/example/Gemfile +0 -5
  77. data/example/features/support/hooks.rb +0 -4
  78. data/lib/honeydew/hooks.rb +0 -0
  79. data/lib/honeydew/step_definitions.rb +0 -75
  80. data/lib/tasks/honeydew.rake +0 -7
  81. data/todo.md +0 -9
@@ -4,148 +4,80 @@ module Honeydew
4
4
  module DeviceActions
5
5
  include Honeydew::DeviceCommands
6
6
 
7
- def click_button(button_text)
8
- perform_action :action => 'click', :arguments => {:text => button_text, :type => 'Button'}
7
+ def scroll_to_text text, index = '0'
8
+ perform_action :scroll_to_text_by_index, :text => text, :index => index
9
9
  end
10
10
 
11
- def click_button_and_wait(button_text)
12
- perform_action :action => 'click_and_wait_for_new_window', :arguments => {:text => button_text, :type => 'Button'}
11
+ def click_button button_text
12
+ perform_action :click, :text => button_text, :type => 'Button'
13
13
  end
14
14
 
15
- def click_text(text)
16
- perform_action :action => 'click', :arguments => {:text => text, :type => 'TextView'}
15
+ def click_button_and_wait button_text
16
+ perform_action :click_and_wait_for_new_window, :text => button_text, :type => 'Button'
17
17
  end
18
18
 
19
- def click_text_and_wait(text)
20
- perform_action :action => 'click_and_wait_for_new_window', :arguments => {:text => text, :type => 'TextView'}
19
+ def click_text text
20
+ perform_action :click, :text => text, :type => 'TextView'
21
21
  end
22
22
 
23
- def click_element(element_description)
24
- perform_action :action => 'click', :arguments => {:description => element_description}
23
+ def click_text_and_wait text
24
+ perform_action :click_and_wait_for_new_window, :text => text, :type => 'TextView'
25
25
  end
26
26
 
27
- def click_element_and_wait(element_description)
28
- perform_action :action => 'click_and_wait_for_new_window', :arguments => {:description => element_description}
27
+ def click_element element_description
28
+ perform_action :click, :description => element_description
29
+ end
30
+
31
+ def click_element_and_wait element_description
32
+ perform_action :click_and_wait_for_new_window, :description => element_description
29
33
  end
30
34
 
31
35
  def fill_in field_description, options = {with: ''}
32
- perform_action :action => 'set_text', :arguments => {:description => field_description, :text => options[:with]}
36
+ perform_action :set_text, :description => field_description, :text => options[:with]
33
37
  end
34
38
 
35
39
  def fill_in_by_label field_label, options = {with: ''}
36
- perform_action :action => 'set_text_by_label', :arguments => {:label => field_label, :text => options[:with]}
40
+ perform_action :set_text_by_label, :label => field_label, :text => options[:with]
37
41
  end
38
42
 
39
43
  def fill_in_by_index index, options = {with: ''}
40
- perform_action :action => 'set_text_by_index', :arguments => {:index => index, :text => options[:with]}
44
+ perform_action :set_text_by_index, :index => index, :text => options[:with]
41
45
  end
42
46
 
43
47
  def launch_home
44
- perform_action :action => 'launch_home'
48
+ perform_action :launch_home
45
49
  end
46
50
 
47
- def launch_settings_app(app_name)
48
- perform_action :action => 'select_from_apps_list', :arguments => {:appName => app_name}
51
+ def launch_settings_app app_name
52
+ perform_action :select_from_apps_list, :appName => app_name
49
53
  end
50
54
 
51
- def launch_application(application_name)
52
- perform_action :action => 'launch_app', :arguments => {:appName => application_name}, :attempts => 3
55
+ def launch_application application_name
56
+ perform_action :launch_app, :appName => application_name
53
57
  end
54
58
 
55
- def long_click_element(element_description)
56
- perform_action :action => 'long_click', :arguments => {:description => element_description}
59
+ def long_click_element element_description
60
+ perform_action :long_click, :description => element_description
57
61
  end
58
62
 
59
- def launch_settings_item(item_name)
60
- perform_action :action => 'select_menu_in_settings', :arguments => {:menuName => item_name}
63
+ def launch_settings_item item_name
64
+ perform_action :select_menu_in_settings, :menuName => item_name
61
65
  end
62
66
 
63
-
64
67
  def press_back
65
- perform_action :action => 'press_back'
68
+ perform_action :press_back
66
69
  end
67
70
 
68
71
  def press_enter
69
- perform_action :action => 'press_enter'
72
+ perform_action :press_enter
70
73
  end
71
74
 
72
75
  def unlock
73
- perform_action :action => 'unlock'
76
+ perform_action :unlock
74
77
  end
75
78
 
76
79
  def wake_up
77
- perform_action :action => 'wake_up'
78
- end
79
-
80
- def perform_action(options)
81
- ensure_tablet_ready
82
-
83
- command = options.slice(:action, :arguments)
84
- timeout = options[:retry_until]
85
- attempts = options[:attempts]
86
-
87
- response =
88
- if timeout
89
- retry_until_timeout(timeout, command)
90
- elsif attempts
91
- retry_until_success(attempts, command)
92
- else
93
- execute_command(command)
94
- end
95
- log_action(command, response)
96
- raise "Device: #{serial} :Action #{options} failed." unless response['success']
97
- response
98
- end
99
-
100
- private
101
-
102
- def retry_until_success(attempts, command)
103
- completed = false
104
- response = nil
105
- tries = 0
106
- until completed || (tries >= attempts) do
107
- response = execute_command(command)
108
- completed = response['success']
109
- return response if completed
110
- tries += 1
111
- sleep 1
112
- end
113
- log_action(command, response)
114
- raise "Device: #{serial} : All #{attempts} attempts failed while performing #{command[:action]}, with arguments: #{command[:arguments]}"
115
- end
116
-
117
- def retry_until_timeout(timeout, command)
118
- completed = false
119
- response = nil
120
- Timeout.timeout(timeout.to_i) do
121
- until completed do
122
- sleep 1
123
- response = execute_command(command)
124
- completed = response['success']
125
- end
126
- end
127
- return response
128
- rescue Timeout::Error
129
- log_action(command, response)
130
- raise "Device: #{serial} :Timeout while performing #{command[:action]}, with arguments: #{command[:arguments]}"
131
- end
132
-
133
- def execute_command(command)
134
- response = RestClient.get(device_endpoint, :params => stringify_keys(:command => command))
135
- JSON.parse(response)
136
- end
137
-
138
- def log_action(command, response)
139
- log "Device: #{serial} : executing command: #{command}"
140
- log "Device: #{serial} : responded with: #{response}"
141
- end
142
-
143
- def stringify_keys(options)
144
- JSON.parse(options.to_json)
145
- end
146
-
147
- def log(message)
148
- puts message
80
+ perform_action :wake_up
149
81
  end
150
82
  end
151
83
  end
@@ -1,12 +1,24 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
1
4
  module Honeydew
2
5
  module DeviceCommands
3
6
 
4
- def device_endpoint
5
- "http://127.0.0.1:#{port}"
7
+ def is_app_installed?(package_name)
8
+ has_app_installed?(package_name)
9
+ end
10
+
11
+ def clear_directory(directory)
12
+ all_files_in_directory_path = [directory.chomp('/'), '/*'].join
13
+ adb "shell rm -r #{all_files_in_directory_path}"
14
+ end
15
+
16
+ def device_endpoint action
17
+ URI.join("http://localhost:#{port}", action)
6
18
  end
7
19
 
8
20
  def dump_window_hierarchy(local_path)
9
- path_in_device = perform_action(:action => 'dump_window_hierarchy')['description']
21
+ path_in_device = perform_action('dump_window_hierarchy')['description']
10
22
  adb "pull #{path_in_device} #{local_path}"
11
23
  end
12
24
 
@@ -16,41 +28,39 @@ module Honeydew
16
28
  adb "pull #{path_in_device} #{local_path}"
17
29
  end
18
30
 
19
- def start_uiautomator_server
31
+ def start_honeydew_server
20
32
  forwarding_port
21
- terminate_uiautomator_server
22
- log "Device: #{serial} : Starting server on the device"
33
+ terminate_honeydew_server
23
34
  start_automation_server
24
35
  end
25
36
 
26
- def terminate_uiautomator_server
27
- log 'Terminating server'
28
- RestClient.get("#{device_endpoint}/terminate")
29
- rescue
37
+ def terminate_honeydew_server
38
+ log "terminating honeydew-server"
39
+ Net::HTTP.post_form device_endpoint('/terminate'), {}
40
+ rescue Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError
30
41
  # Swallow
31
42
  end
32
43
 
33
- def automation_test_jar_name
34
- "android-server-0.0.1-SNAPSHOT.jar"
44
+ def honeydew_server_package
45
+ "honeydew-server-#{Honeydew::VERSION}.jar"
35
46
  end
36
47
 
37
- def automation_server_jar_path
38
- File.absolute_path(File.join(File.dirname(__FILE__), "../../android-server/target/#{automation_test_jar_name}"))
48
+ def honeydew_server_file
49
+ File.absolute_path(File.join(File.dirname(__FILE__), "../../server/target/#{honeydew_server_package}"))
39
50
  end
40
51
 
41
52
  def start_automation_server
53
+ log "starting honeydew-server on the device"
42
54
  Thread.new do
43
- adb "push #{automation_server_jar_path} /data/local/tmp"
44
- adb "shell uiautomator runtest #{automation_test_jar_name} -c com.uiautomator_cucumber.android_server.TestRunner"
45
- log "Device: #{serial} initiated the start of automation server"
55
+ adb "push #{honeydew_server_file} /data/local/tmp"
56
+ adb "shell uiautomator runtest #{honeydew_server_package} -c com.amplify.honeydew_server.TestRunner"
46
57
  end
47
58
  at_exit do
48
- terminate_uiautomator_server
59
+ terminate_honeydew_server
49
60
  end
50
61
  end
51
62
 
52
63
  def forwarding_port
53
- log "Device: #{serial} : Forwarding port #{port} to device"
54
64
  adb "forward tcp:#{port} tcp:7120"
55
65
  end
56
66
 
@@ -76,9 +86,12 @@ module Honeydew
76
86
 
77
87
  def adb(command)
78
88
  adb_command = "adb -s #{serial} #{command}"
79
- log "Device: #{serial} :Executing '#{adb_command}'"
80
- `#{adb_command}`.tap { raise 'ADB command failed' unless $?.success? }
89
+ log "executing '#{adb_command}'"
90
+ `#{adb_command} 2>/dev/null`.tap do
91
+ if $?.exitstatus != 0
92
+ raise "ADB command '#{command}' failed"
93
+ end
94
+ end
81
95
  end
82
-
83
96
  end
84
- end
97
+ end
@@ -1,36 +1,40 @@
1
1
  module Honeydew
2
2
  module DeviceMatchers
3
-
4
- def has_text?(text, timeout = Honeydew.config.timeout)
5
- perform_action :action => 'is_text_present', :arguments => {:text => text}, :retry_until => timeout
3
+ def has_text?(text)
4
+ perform_assertion :is_text_present, :text => text
6
5
  end
7
6
 
8
- def has_element_with_description?(description, timeout = Honeydew.config.timeout)
9
- perform_action :action => 'is_text_present', :arguments => {:description => description}, :retry_until => timeout
7
+ def has_element_with_description?(description)
8
+ perform_assertion :is_text_present, :description => description
10
9
  end
11
10
 
12
- def has_edit_text?(text, timeout = Honeydew.config.timeout)
13
- perform_action :action => 'is_text_present', :arguments => {:text => text, :type => 'EditText'}, :retry_until => timeout
11
+ def has_edit_text?(text)
12
+ perform_assertion :is_text_present, :text => text, :type => 'EditText'
14
13
  end
15
14
 
16
- def has_textview_text?(text, timeout = Honeydew.config.timeout)
17
- perform_action :action => 'is_text_present', :arguments => {:text => text, :type => 'TextView'}, :retry_until => timeout
15
+ def has_textview_text?(text)
16
+ perform_assertion :is_text_present, :text => text, :type => 'TextView'
18
17
  end
19
18
 
20
- def has_textview_with_text_and_description?(text, description, timeout=Honeydew.config.timeout)
21
- perform_action :action => 'is_text_present', :arguments => {:text => text, :description => description, :type => 'TextView'}, :retry_until => timeout
19
+ def has_textview_with_text_and_description?(text, description)
20
+ perform_assertion :is_text_present, :text => text, :description => description, :type => 'TextView'
22
21
  end
23
22
 
24
- def has_button?(button_text, timeout = Honeydew.config.timeout)
25
- perform_action(:action => 'is_button_present', :arguments => {:text => button_text}, :retry_until => timeout)
23
+ def has_button?(button_text)
24
+ perform_assertion :is_button_present, :text => button_text
26
25
  end
27
26
 
28
- def has_child_count?(parent_element_description, child_element_description, child_count, timeout = Honeydew.config.timeout)
29
- perform_action(:action => 'is_child_count_equal_to', :arguments => {:parent_description => parent_element_description, :child_description => child_element_description, :child_count => child_count}, :retry_until => timeout)
27
+ def has_child_count?(parent_element_description, child_element_description, child_count)
28
+ perform_assertion :is_child_count_equal_to,
29
+ :parent_description => parent_element_description,
30
+ :child_description => child_element_description,
31
+ :child_count => child_count
30
32
  end
31
33
 
32
- def has_element_with_nested_text?(parent_description, child_text, timeout = Honeydew.config.timeout)
33
- perform_action(:action => 'is_element_with_nested_text_present', :arguments => {:parent_description => parent_description, :child_text => child_text}, :retry_until => timeout)
34
+ def has_element_with_nested_text?(parent_description, child_text)
35
+ perform_assertion :is_element_with_nested_text_present,
36
+ :parent_description => parent_description,
37
+ :child_text => child_text
34
38
  end
35
39
 
36
40
  def has_app_installed?(package_name)
@@ -38,15 +42,18 @@ module Honeydew
38
42
  end
39
43
 
40
44
  def is_option_in_setting_enabled?(item_name, option_names)
41
- perform_action :action => 'is_option_in_settings_menu_enabled', :arguments => {:menuName => item_name, :optionNames => option_names}
45
+ perform_assertion :is_option_in_settings_menu_enabled,
46
+ :menuName => item_name, :optionNames => option_names
42
47
  end
43
48
 
44
49
  def is_option_in_setting_disabled?(item_name, option_names)
45
- perform_action :action => 'is_option_in_settings_menu_disabled', :arguments => {:menuName => item_name, :optionNames => option_names}
50
+ perform_assertion :is_option_in_settings_menu_disabled,
51
+ :menuName => item_name, :optionNames => option_names
46
52
  end
47
53
 
48
- def has_settings_menu_item?(item_name, timeout = 10)
49
- perform_action(:action => 'has_settings_menu_item', :arguments => {:menuName => item_name}, :retry_until => timeout)
54
+ def has_settings_menu_item?(item_name)
55
+ perform_assertion :has_settings_menu_item,
56
+ :menuName => item_name
50
57
  end
51
58
  end
52
- end
59
+ end
data/lib/honeydew/dsl.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Honeydew
2
2
  module DSL
3
- def use_device(name, serial)
3
+ def use_device(name, serial = nil)
4
4
  define_singleton_method name.to_sym do |*args, &block|
5
5
  Honeydew.using_device(serial, &block)
6
6
  end
@@ -0,0 +1,82 @@
1
+ require 'active_support/core_ext'
2
+
3
+ require 'honeydew/version'
4
+ require 'honeydew/device'
5
+
6
+ module Honeydew
7
+ class Configuration
8
+ attr_accessor :debug,
9
+ :server_timeout,
10
+ :timeout,
11
+ :port
12
+
13
+ def initialize
14
+ @port = 7120
15
+ @debug = true
16
+ @timeout = 5.seconds
17
+ @server_timeout = 30.seconds
18
+ end
19
+
20
+ def obtain_new_port
21
+ @port.tap { @port += 1 }
22
+ end
23
+ end
24
+
25
+ class <<self
26
+ attr_accessor :config
27
+
28
+ def config
29
+ return @config if @config
30
+ configure
31
+ @config
32
+ end
33
+
34
+ def configure
35
+ @config ||= Configuration.new
36
+ yield(@config) if block_given?
37
+ end
38
+
39
+ def attached_devices
40
+ @attached_devices ||= begin
41
+ `adb devices`.split("\n").drop(1).collect {|line| line.split[0].chomp}
42
+ end
43
+ end
44
+
45
+ def default_device_serial
46
+ attached_devices.first
47
+ end
48
+
49
+ def default_device
50
+ @default_device ||= device[default_device_serial]
51
+ end
52
+
53
+ def current_device
54
+ @current_device ||= default_device
55
+ end
56
+
57
+ def using_device(serial, &block)
58
+ original_device = current_device
59
+ use_device(serial || Honeydew.default_device_serial).tap do |device|
60
+ device.instance_eval(&block) if block_given?
61
+ end
62
+ ensure
63
+ @current_device = original_device
64
+ end
65
+
66
+ def devices
67
+ @devices
68
+ end
69
+
70
+ private
71
+
72
+ def use_device(serial)
73
+ @current_device = device[serial]
74
+ end
75
+
76
+ def device
77
+ @devices ||= Hash.new do |hash, serial|
78
+ hash[serial] = Device.new(serial)
79
+ end
80
+ end
81
+ end
82
+ end