Home | History | Annotate | Download | only in extensions
      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/extensions/browser_action_test_util.h"
      6 
      7 #include <gtk/gtk.h>
      8 
      9 #include "chrome/browser/ui/browser.h"
     10 #include "chrome/browser/ui/browser_window.h"
     11 #include "chrome/browser/ui/gtk/extensions/extension_popup_gtk.h"
     12 #include "chrome/browser/ui/gtk/extensions/extension_view_gtk.h"
     13 #include "chrome/browser/ui/gtk/view_id_util.h"
     14 #include "ui/gfx/image/image.h"
     15 
     16 namespace {
     17 
     18 GtkWidget* GetButton(Browser* browser, int index) {
     19   GtkWidget* toolbar =
     20       ViewIDUtil::GetWidget(GTK_WIDGET(browser->window()->GetNativeWindow()),
     21                             VIEW_ID_BROWSER_ACTION_TOOLBAR);
     22   GtkWidget* button = NULL;
     23   if (toolbar) {
     24     GList* children = gtk_container_get_children(GTK_CONTAINER(toolbar));
     25     GtkWidget* alignment =
     26         static_cast<GtkWidget*>(g_list_nth(children, index)->data);
     27     button = gtk_bin_get_child(GTK_BIN(alignment));
     28     g_list_free(children);
     29   }
     30   return button;
     31 }
     32 
     33 }  // namespace
     34 
     35 int BrowserActionTestUtil::NumberOfBrowserActions() {
     36   int count = -1;
     37   GtkWidget* toolbar =
     38       ViewIDUtil::GetWidget(GTK_WIDGET(browser_->window()->GetNativeWindow()),
     39                             VIEW_ID_BROWSER_ACTION_TOOLBAR);
     40   if (toolbar) {
     41     GList* children = gtk_container_get_children(GTK_CONTAINER(toolbar));
     42     count = g_list_length(children);
     43     g_list_free(children);
     44   }
     45   return count;
     46 }
     47 
     48 bool BrowserActionTestUtil::HasIcon(int index) {
     49   GtkWidget* button = GetButton(browser_, index);
     50   return gtk_button_get_image(GTK_BUTTON(button)) != NULL;
     51 }
     52 
     53 gfx::Image BrowserActionTestUtil::GetIcon(int index) {
     54   GtkWidget* button = GetButton(browser_, index);
     55   GtkWidget* image = gtk_button_get_image(GTK_BUTTON(button));
     56   GdkPixbuf* pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(image));
     57   // gfx::Image takes ownership of the |pixbuf| reference. We have to increase
     58   // the ref count so |pixbuf| stays around when the image object is destroyed.
     59   g_object_ref(pixbuf);
     60   return gfx::Image(pixbuf);
     61 }
     62 
     63 void BrowserActionTestUtil::Press(int index) {
     64   GtkWidget* button = GetButton(browser_, index);
     65   gtk_button_clicked(GTK_BUTTON(button));
     66 }
     67 
     68 std::string BrowserActionTestUtil::GetTooltip(int index) {
     69   GtkWidget* button = GetButton(browser_, index);
     70   gchar* text = gtk_widget_get_tooltip_text(button);
     71   std::string tooltip(text);
     72   g_free(text);
     73   return tooltip;
     74 }
     75 
     76 bool BrowserActionTestUtil::HasPopup() {
     77   return ExtensionPopupGtk::get_current_extension_popup() != NULL;
     78 }
     79 
     80 gfx::Rect BrowserActionTestUtil::GetPopupBounds() {
     81   ExtensionPopupGtk* popup = ExtensionPopupGtk::get_current_extension_popup();
     82   if (popup)
     83     return popup->GetViewBounds();
     84   return gfx::Rect();
     85 }
     86 
     87 bool BrowserActionTestUtil::HidePopup() {
     88   ExtensionPopupGtk* popup = ExtensionPopupGtk::get_current_extension_popup();
     89   if (popup)
     90     return popup->DestroyPopup();
     91   return false;
     92 }
     93 
     94 // static
     95 gfx::Size BrowserActionTestUtil::GetMinPopupSize() {
     96   // On Linux we actually just limit the size of the extension view.
     97   return gfx::Size(ExtensionPopupGtk::kMinWidth, ExtensionPopupGtk::kMinHeight);
     98 }
     99 
    100 // static
    101 gfx::Size BrowserActionTestUtil::GetMaxPopupSize() {
    102   return gfx::Size(ExtensionPopupGtk::kMaxWidth, ExtensionPopupGtk::kMaxHeight);
    103 }
    104