Home | History | Annotate | Download | only in views
      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/ui/views/toolbar_view.h"
      6 
      7 #include "base/strings/utf_string_conversions.h"
      8 #include "chrome/app/chrome_command_ids.h"
      9 #include "chrome/browser/bookmarks/bookmark_model.h"
     10 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
     11 #include "chrome/browser/bookmarks/bookmark_utils.h"
     12 #include "chrome/browser/ui/browser.h"
     13 #include "chrome/browser/ui/browser_command_controller.h"
     14 #include "chrome/browser/ui/browser_window.h"
     15 #include "chrome/browser/ui/view_ids.h"
     16 #include "chrome/browser/ui/views/frame/browser_view.h"
     17 #include "chrome/test/base/in_process_browser_test.h"
     18 #include "ui/views/focus/focus_manager.h"
     19 #include "ui/views/view.h"
     20 #include "ui/views/widget/widget.h"
     21 
     22 namespace {
     23 
     24 class ToolbarViewTest : public InProcessBrowserTest {
     25  public:
     26   ToolbarViewTest() {}
     27 
     28   void RunToolbarCycleFocusTest(Browser* browser);
     29 
     30  private:
     31   DISALLOW_COPY_AND_ASSIGN(ToolbarViewTest);
     32 };
     33 
     34 void ToolbarViewTest::RunToolbarCycleFocusTest(Browser* browser) {
     35   gfx::NativeWindow window = browser->window()->GetNativeWindow();
     36   views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
     37   views::FocusManager* focus_manager = widget->GetFocusManager();
     38   CommandUpdater* updater = browser->command_controller()->command_updater();
     39 
     40   // Send focus to the toolbar as if the user pressed Alt+Shift+T.
     41   updater->ExecuteCommand(IDC_FOCUS_TOOLBAR);
     42 
     43   views::View* first_view = focus_manager->GetFocusedView();
     44   std::vector<int> ids;
     45 
     46   // Press Tab to cycle through all of the controls in the toolbar until
     47   // we end up back where we started.
     48   bool found_reload = false;
     49   bool found_location_bar = false;
     50   bool found_app_menu = false;
     51   const views::View* view = NULL;
     52   while (view != first_view) {
     53     focus_manager->AdvanceFocus(false);
     54     view = focus_manager->GetFocusedView();
     55     ids.push_back(view->id());
     56     if (view->id() == VIEW_ID_RELOAD_BUTTON)
     57       found_reload = true;
     58     if (view->id() == VIEW_ID_APP_MENU)
     59       found_app_menu = true;
     60     if (view->id() == VIEW_ID_OMNIBOX)
     61       found_location_bar = true;
     62     if (ids.size() > 100)
     63       GTEST_FAIL() << "Tabbed 100 times, still haven't cycled back!";
     64   }
     65 
     66   // Make sure we found a few key items.
     67   ASSERT_TRUE(found_reload);
     68   ASSERT_TRUE(found_app_menu);
     69   ASSERT_TRUE(found_location_bar);
     70 
     71   // Now press Shift-Tab to cycle backwards.
     72   std::vector<int> reverse_ids;
     73   view = NULL;
     74   while (view != first_view) {
     75     focus_manager->AdvanceFocus(true);
     76     view = focus_manager->GetFocusedView();
     77     reverse_ids.push_back(view->id());
     78     if (reverse_ids.size() > 100)
     79       GTEST_FAIL() << "Tabbed 100 times, still haven't cycled back!";
     80   }
     81 
     82   // Assert that the views were focused in exactly the reverse order.
     83   // The sequences should be the same length, and the last element will
     84   // be the same, and the others are reverse.
     85   ASSERT_EQ(ids.size(), reverse_ids.size());
     86   size_t count = ids.size();
     87   for (size_t i = 0; i < count - 1; i++)
     88     EXPECT_EQ(ids[i], reverse_ids[count - 2 - i]);
     89 }
     90 
     91 // The test is flaky on Win (http://crbug.com/152938) and crashes on CrOS under
     92 // AddressSanitizer (http://crbug.com/154657).
     93 IN_PROC_BROWSER_TEST_F(ToolbarViewTest, DISABLED_ToolbarCycleFocus) {
     94   RunToolbarCycleFocusTest(browser());
     95 }
     96 
     97 #if defined(OS_WIN)
     98 // http://crbug.com/152938 Flaky on win.
     99 #define MAYBE_ToolbarCycleFocusWithBookmarkBar \
    100     DISABLED_ToolbarCycleFocusWithBookmarkBar
    101 #else
    102 #define MAYBE_ToolbarCycleFocusWithBookmarkBar ToolbarCycleFocusWithBookmarkBar
    103 #endif
    104 IN_PROC_BROWSER_TEST_F(ToolbarViewTest,
    105                        MAYBE_ToolbarCycleFocusWithBookmarkBar) {
    106   CommandUpdater* updater = browser()->command_controller()->command_updater();
    107   updater->ExecuteCommand(IDC_SHOW_BOOKMARK_BAR);
    108 
    109   BookmarkModel* model =
    110       BookmarkModelFactory::GetForProfile(browser()->profile());
    111   bookmark_utils::AddIfNotBookmarked(
    112       model, GURL("http://foo.com"), ASCIIToUTF16("Foo"));
    113 
    114   // We want to specifically test the case where the bookmark bar is
    115   // already showing when a window opens, so create a second browser
    116   // window with the same profile.
    117   Browser* second_browser = CreateBrowser(browser()->profile());
    118   RunToolbarCycleFocusTest(second_browser);
    119 }
    120 
    121 }  // namespace
    122