// Copyright (c) 2012 Jonathan Penn (http://cocoamanifest.net/)

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// captureLocalizedScreenshot(name)
//
// Tells the local target to take a screen shot and names the file after the
// state of the simulator like so:
//
//   [model]___[orientation]___[name].png
//
// `model` is the model of the device (iphone, iphone5, ipad)
// `orientation` is...well...duh!
// `name` is what you passed in to the function
//
// Screenshots are saved along with the trace results in UI Automation. See
// `run_screenshooter.sh` for examples on how to pull those images out and put
// them wherever you want.
//
function captureLocalizedScreenshot(name) {
  var target = UIATarget.localTarget();
  var model = target.model();
  var rect = target.rect();

  if (model.match(/iPhone/)) {
    if (rect.size.height > 480) model = "iOS-4-in";
    else model = "iOS-3.5-in";
  } else {
    model = "iOS-iPad";
  }

  var orientation = "portrait";
  if (rect.size.height < rect.size.width) orientation = "landscape";

  var parts = [model, orientation, name];
  target.captureScreenWithName(parts.join("___"));
}


@@ -1807,260 +1807,350 @@
 %0A%0A  
-if (model.match(/iPhone/)) %7B%0A    if (rect.size.height %3E 480) model = %22iOS-4-in%22;%0A    else model = %22iOS-3.5-in%22;%0A  %7D else %7B%0A    model = %22iOS-iPad%22;%0A  %7D%0A%0A  var orientation = %22portrait%22;%0A  if (rect.size.height %3C rect.size.width) orientation = %22landscape%22;
+var orientation = %22portrait%22;%0A  if (rect.size.height %3C rect.size.width) orientation = %22landscape%22;%0A  %0A  if (model.match(/iPhone/)) %7B%0A    if ((orientation == %22portrait%22 && rect.size.height %3E 480) %7C%7C (orientation == %22landscape%22 && rect.size.width %3E 480)) model = %22iOS-4-in%22;%0A    else model = %22iOS-3.5-in%22;%0A  %7D else %7B%0A    model = %22iOS-iPad%22;%0A  %7D
 %0A%0A  

Correct model when capturing landscape