Home | History | Annotate | Download | only in automation
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/automation/testing_automation_provider.h"
      6 
      7 #import <Cocoa/Cocoa.h>
      8 
      9 #include "ui/base/l10n/l10n_util.h"
     10 #include "base/logging.h"
     11 #include "base/strings/sys_string_conversions.h"
     12 #include "chrome/browser/automation/automation_browser_tracker.h"
     13 #include "chrome/browser/automation/automation_window_tracker.h"
     14 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
     15 #include "chrome/browser/ui/cocoa/tabs/tab_window_controller.h"
     16 #include "chrome/browser/ui/view_ids.h"
     17 #include "grit/generated_resources.h"
     18 #include "ui/base/l10n/l10n_util_mac.h"
     19 
     20 void TestingAutomationProvider::TerminateSession(int handle, bool* success) {
     21   *success = false;
     22   NOTIMPLEMENTED();
     23 }
     24 
     25 void TestingAutomationProvider::WindowGetViewBounds(int handle,
     26                                                     int view_id,
     27                                                     bool screen_coordinates,
     28                                                     bool* success,
     29                                                     gfx::Rect* bounds) {
     30   *success = false;
     31 
     32   // At the moment we hard code the view ID used by WebDriver and do
     33   // not support arbitrary view IDs.  suzhe is working on general view
     34   // ID support for the Mac.
     35   if (view_id != VIEW_ID_TAB_CONTAINER) {
     36     NOTIMPLEMENTED();
     37     return;
     38   }
     39 
     40   NSWindow* window = window_tracker_->GetResource(handle);
     41   if (!window)
     42     return;
     43 
     44   BrowserWindowController* controller = [window windowController];
     45   DCHECK([controller isKindOfClass:[BrowserWindowController class]]);
     46   if (![controller isKindOfClass:[BrowserWindowController class]])
     47     return;
     48   NSView* tab =
     49       [[[controller tabStripController] activeTabContentsController] view];
     50   if (!tab)
     51     return;
     52 
     53   NSRect rect = [tab convertRect:[tab bounds] toView:nil];
     54   // The origin of the bounding box should be the top left of the tab contents,
     55   // not bottom left, to match other platforms.
     56   rect.origin.y += rect.size.height;
     57   CGFloat coord_sys_height;
     58   if (screen_coordinates && [[NSScreen screens] count] > 0) {
     59     rect.origin = [window convertBaseToScreen:rect.origin];
     60     coord_sys_height =
     61         [[[NSScreen screens] objectAtIndex:0] frame].size.height;
     62   } else {
     63     coord_sys_height = [window frame].size.height;
     64   }
     65   // Flip coordinate system.
     66   rect.origin.y = coord_sys_height - rect.origin.y;
     67   *bounds = gfx::Rect(NSRectToCGRect(rect));
     68   *success = true;
     69 }
     70 
     71 void TestingAutomationProvider::SetWindowBounds(int handle,
     72                                                 const gfx::Rect& bounds,
     73                                                 bool* success) {
     74   *success = false;
     75   NSWindow* window = window_tracker_->GetResource(handle);
     76   if (window) {
     77     NSRect new_bounds = NSRectFromCGRect(bounds.ToCGRect());
     78 
     79     if ([[NSScreen screens] count] > 0) {
     80       new_bounds.origin.y =
     81           [[[NSScreen screens] objectAtIndex:0] frame].size.height -
     82           new_bounds.origin.y - new_bounds.size.height;
     83     }
     84 
     85     [window setFrame:new_bounds display:NO];
     86     *success = true;
     87   }
     88 }
     89