Skip to content
This repository has been archived by the owner on Sep 4, 2018. It is now read-only.

Commit

Permalink
Add support for querying and setting application defaults.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Holland committed Mar 17, 2010
1 parent 188fb48 commit 7387af6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
18 changes: 18 additions & 0 deletions ext/iCuke/iCukeHTTPConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ @implementation iCukeHTTPConnection
} else if ([path hasPrefix:@"/view"]) {
NSData *browseData = [[[Viewer sharedViewer] screen] dataUsingEncoding:NSUTF8StringEncoding];
return [[[HTTPDataResponse alloc] initWithData:browseData] autorelease];
} else if ([path hasPrefix:@"/defaults"]) {
NSUserDefaults *user_defaults = [NSUserDefaults standardUserDefaults];
id json = [[self parseRequestQuery] objectForKey:@"json"];

if (!json) {
NSDictionary *defaults = [user_defaults dictionaryRepresentation];

NSData *browseData = [[defaults JSONRepresentation] dataUsingEncoding:NSUTF8StringEncoding];
return [[[HTTPDataResponse alloc] initWithData:browseData] autorelease];
} else {
id parsed_json = [json JSONValue];

NSEnumerator *enumerator = [parsed_json keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
[user_defaults setObject: [parsed_json objectForKey: key] forKey: key];
}
}
} else if ([path hasPrefix:@"/record"]) {
[[Recorder sharedRecorder] record];

Expand Down
10 changes: 7 additions & 3 deletions lib/icuke/cucumber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def page
end

def response
@response ||= Curl::Easy.http_get(BASE_URL + '/view').body_str
@response ||= @simulator.view
end

def record
Curl::Easy.http_get(BASE_URL + "/record")
@simulator.record
end

def tap(label, options = {}, &block)
Expand All @@ -51,7 +51,7 @@ def tap(label, options = {}, &block)
x = frame['x'].to_f + (frame['width'].to_f / 2)
y = frame['y'].to_f + (frame['height'].to_f / 2)

Curl::Easy.http_get(BASE_URL + "/event?json=#{Tap.new(x, y, options).to_json}")
@simulator.fire_event(Tap.new(x, y, options))

sleep(options[:pause] ? 1 : 0.2)

Expand Down Expand Up @@ -85,6 +85,10 @@ def type(textfield, text)
tap('Return')
end

def set_application_defaults(defaults)
@simulator.set_defaults(defaults)
end

private

def refresh
Expand Down
42 changes: 34 additions & 8 deletions lib/icuke/simulator.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require 'osx/cocoa'
OSX.require_framework '/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/PrivateFrameworks/iPhoneSimulatorRemoteClient.framework'
require 'curb'
require 'httparty'

module ICuke
class Simulator < OSX::NSObject
include OSX

BASE_URL = 'http://localhost:50000'
include HTTParty
base_uri 'http://localhost:50000'

class FailedToStart < RuntimeError; end

Expand Down Expand Up @@ -66,20 +66,46 @@ def launch(application, options = {})
end

def quit
Curl::Easy.http_get(BASE_URL + '/quit')
rescue Curl::Err::GotNothingError
get '/quit'
rescue EOFError
end

def view
get('/view')
end

def record
get '/record'
end

def stop
get '/stop'
end

def save(path)
Curl::Easy.http_get(BASE_URL + "/save?file=#{path}")
get '/save', :query => { :file => path }
end

def load(path)
Curl::Easy.http_get(BASE_URL + "/load?file=#{path}")
get '/load', :query => { :file => path }
end

def play
Curl::Easy.http_get(BASE_URL + '/play')
get '/play'
end

def fire_event(event)
get '/event', :query => { :json => event.to_json }
end

def set_defaults(defaults)
get '/defaults', :query => { :json => defaults.to_json }
end

private

def get(path, options = {})
self.class.get(path, options).body
end
end
end

0 comments on commit 7387af6

Please sign in to comment.