Home | History | Annotate | Download | only in automation
      1 // Copyright (c) 2011 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/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::ActivateWindow(int handle) {
     21   NOTIMPLEMENTED();
     22 }
     23 
     24 void TestingAutomationProvider::IsWindowMaximized(int handle,
     25                                                   bool* is_maximized,
     26                                                   bool* success) {
     27   *success = false;
     28   NSWindow* window = window_tracker_->GetResource(handle);
     29   if (!window)
     30     return;
     31   *is_maximized = [window isZoomed];
     32   *success = true;
     33 }
     34 
     35 void TestingAutomationProvider::TerminateSession(int handle, bool* success) {
     36   *success = false;
     37   NOTIMPLEMENTED();
     38 }
     39 
     40 void TestingAutomationProvider::WindowGetViewBounds(int handle,
     41                                                     int view_id,
     42                                                     bool screen_coordinates,
     43                                                     bool* success,
     44                                                     gfx::Rect* bounds) {
     45   *success = false;
     46 
     47   // At the moment we hard code the view ID used by WebDriver and do
     48   // not support arbitrary view IDs.  suzhe is working on general view
     49   // ID support for the Mac.
     50   if (view_id != VIEW_ID_TAB_CONTAINER) {
     51     NOTIMPLEMENTED();
     52     return;
     53   }
     54 
     55   NSWindow* window = window_tracker_->GetResource(handle);
     56   if (!window)
     57     return;
     58 
     59   BrowserWindowController* controller = [window windowController];
     60   DCHECK([controller isKindOfClass:[BrowserWindowController class]]);
     61   if (![controller isKindOfClass:[BrowserWindowController class]])
     62     return;
     63   NSView* tab =
     64       [[[controller tabStripController] activeTabContentsController] view];
     65   if (!tab)
     66     return;
     67 
     68   NSRect rect = [tab convertRectToBase:[tab bounds]];
     69   // The origin of the bounding box should be the top left of the tab contents,
     70   // not bottom left, to match other platforms.
     71   rect.origin.y += rect.size.height;
     72   CGFloat coord_sys_height;
     73   if (screen_coordinates && [[NSScreen screens] count] > 0) {
     74     rect.origin = [window convertBaseToScreen:rect.origin];
     75     coord_sys_height =
     76         [[[NSScreen screens] objectAtIndex:0] frame].size.height;
     77   } else {
     78     coord_sys_height = [window frame].size.height;
     79   }
     80   // Flip coordinate system.
     81   rect.origin.y = coord_sys_height - rect.origin.y;
     82   *bounds = gfx::Rect(NSRectToCGRect(rect));
     83   *success = true;
     84 }
     85 
     86 void TestingAutomationProvider::GetWindowBounds(int handle,
     87                                                 gfx::Rect* bounds,
     88                                                 bool* success) {
     89   *success = false;
     90   NSWindow* window = window_tracker_->GetResource(handle);
     91   if (window) {
     92     // Move rect to reflect flipped coordinate system.
     93     if ([[NSScreen screens] count] > 0) {
     94       NSRect rect = [window frame];
     95       rect.origin.y =
     96           [[[NSScreen screens] objectAtIndex:0] frame].size.height -
     97               rect.origin.y - rect.size.height;
     98       *bounds = gfx::Rect(NSRectToCGRect(rect));
     99       *success = true;
    100     }
    101   }
    102 }
    103 
    104 void TestingAutomationProvider::SetWindowBounds(int handle,
    105                                                 const gfx::Rect& bounds,
    106                                                 bool* success) {
    107   *success = false;
    108   NSWindow* window = window_tracker_->GetResource(handle);
    109   if (window) {
    110     NSRect new_bounds = NSRectFromCGRect(bounds.ToCGRect());
    111 
    112     if ([[NSScreen screens] count] > 0) {
    113       new_bounds.origin.y =
    114           [[[NSScreen screens] objectAtIndex:0] frame].size.height -
    115           new_bounds.origin.y - new_bounds.size.height;
    116     }
    117 
    118     [window setFrame:new_bounds display:NO];
    119     *success = true;
    120   }
    121 }
    122 
    123 void TestingAutomationProvider::SetWindowVisible(int handle,
    124                                                  bool visible,
    125                                                  bool* result) {
    126   *result = false;
    127   NSWindow* window = window_tracker_->GetResource(handle);
    128   if (window) {
    129     if (visible) {
    130       [window orderFront:nil];
    131     } else {
    132       [window orderOut:nil];
    133     }
    134     *result = true;
    135   }
    136 }
    137 
    138 void TestingAutomationProvider::GetWindowTitle(int handle, string16* text) {
    139   gfx::NativeWindow window = window_tracker_->GetResource(handle);
    140   NSString* title = nil;
    141   if ([[window delegate] isKindOfClass:[TabWindowController class]]) {
    142     TabWindowController* delegate =
    143         reinterpret_cast<TabWindowController*>([window delegate]);
    144     title = [delegate selectedTabTitle];
    145   } else {
    146     title = [window title];
    147   }
    148   // If we don't yet have a title, use "Untitled".
    149   if (![title length]) {
    150     text->assign(l10n_util::GetStringUTF16(
    151         IDS_BROWSER_WINDOW_MAC_TAB_UNTITLED));
    152     return;
    153   }
    154 
    155   text->assign(base::SysNSStringToUTF16(title));
    156 }
    157