Home | History | Annotate | Download | only in cocoa
      1 // Copyright (c) 2010 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 "base/basictypes.h"
      6 #include "base/command_line.h"
      7 #include "base/utf_string_conversions.h"
      8 #include "chrome/browser/bookmarks/bookmark_model.h"
      9 #include "chrome/browser/download/download_shelf.h"
     10 #include "chrome/browser/prefs/pref_service.h"
     11 #include "chrome/browser/profiles/profile.h"
     12 #include "chrome/browser/sidebar/sidebar_manager.h"
     13 #include "chrome/browser/ui/browser.h"
     14 #include "chrome/browser/ui/browser_window.h"
     15 #include "chrome/browser/ui/cocoa/view_id_util.h"
     16 #include "chrome/common/chrome_switches.h"
     17 #include "chrome/common/pref_names.h"
     18 #include "chrome/common/url_constants.h"
     19 #include "chrome/test/in_process_browser_test.h"
     20 #include "chrome/test/ui_test_utils.h"
     21 
     22 // Basic sanity check of ViewID use on the mac.
     23 class ViewIDTest : public InProcessBrowserTest {
     24  public:
     25   ViewIDTest() : root_window_(nil) {
     26     CommandLine::ForCurrentProcess()->AppendSwitch(
     27         switches::kEnableExperimentalExtensionApis);
     28   }
     29 
     30   void CheckViewID(ViewID view_id, bool should_have) {
     31     if (!root_window_)
     32       root_window_ = browser()->window()->GetNativeHandle();
     33 
     34     ASSERT_TRUE(root_window_);
     35     NSView* view = view_id_util::GetView(root_window_, view_id);
     36     EXPECT_EQ(should_have, !!view) << " Failed id=" << view_id;
     37   }
     38 
     39   void DoTest() {
     40     // Make sure FindBar is created to test
     41     // VIEW_ID_FIND_IN_PAGE_TEXT_FIELD and VIEW_ID_FIND_IN_PAGE.
     42     browser()->ShowFindBar();
     43 
     44     // Make sure sidebar is created to test VIEW_ID_SIDE_BAR_CONTAINER.
     45     const char sidebar_content_id[] = "test_content_id";
     46     SidebarManager::GetInstance()->ShowSidebar(
     47         browser()->GetSelectedTabContents(), sidebar_content_id);
     48     SidebarManager::GetInstance()->ExpandSidebar(
     49         browser()->GetSelectedTabContents(), sidebar_content_id);
     50 
     51     // Make sure docked devtools is created to test VIEW_ID_DEV_TOOLS_DOCKED
     52     browser()->profile()->GetPrefs()->SetBoolean(prefs::kDevToolsOpenDocked,
     53                                                  true);
     54     browser()->ToggleDevToolsWindow(DEVTOOLS_TOGGLE_ACTION_INSPECT);
     55 
     56     // Make sure download shelf is created to test VIEW_ID_DOWNLOAD_SHELF
     57     browser()->window()->GetDownloadShelf()->Show();
     58 
     59     // Create a bookmark to test VIEW_ID_BOOKMARK_BAR_ELEMENT
     60     BookmarkModel* bookmark_model = browser()->profile()->GetBookmarkModel();
     61     if (bookmark_model) {
     62       if (!bookmark_model->IsLoaded())
     63         ui_test_utils::WaitForBookmarkModelToLoad(bookmark_model);
     64 
     65       bookmark_model->SetURLStarred(GURL(chrome::kAboutBlankURL),
     66                                     UTF8ToUTF16("about"), true);
     67     }
     68 
     69     for (int i = VIEW_ID_TOOLBAR; i < VIEW_ID_PREDEFINED_COUNT; ++i) {
     70       // Mac implementation does not support following ids yet.
     71       if (i == VIEW_ID_STAR_BUTTON ||
     72           i == VIEW_ID_AUTOCOMPLETE ||
     73           i == VIEW_ID_CONTENTS_SPLIT ||
     74           i == VIEW_ID_SIDE_BAR_SPLIT ||
     75           i == VIEW_ID_FEEDBACK_BUTTON) {
     76         continue;
     77       }
     78 
     79       CheckViewID(static_cast<ViewID>(i), true);
     80     }
     81 
     82     CheckViewID(VIEW_ID_TAB, true);
     83     CheckViewID(VIEW_ID_TAB_STRIP, true);
     84     CheckViewID(VIEW_ID_PREDEFINED_COUNT, false);
     85   }
     86 
     87  private:
     88   NSWindow* root_window_;
     89 };
     90 
     91 IN_PROC_BROWSER_TEST_F(ViewIDTest, Basic) {
     92   ASSERT_NO_FATAL_FAILURE(DoTest());
     93 }
     94 
     95 IN_PROC_BROWSER_TEST_F(ViewIDTest, Fullscreen) {
     96   browser()->window()->SetFullscreen(true);
     97   ASSERT_NO_FATAL_FAILURE(DoTest());
     98 }
     99 
    100 IN_PROC_BROWSER_TEST_F(ViewIDTest, Tab) {
    101   CheckViewID(VIEW_ID_TAB_0, true);
    102   CheckViewID(VIEW_ID_TAB_LAST, true);
    103 
    104   // Open 9 new tabs.
    105   for (int i = 1; i <= 9; ++i) {
    106     CheckViewID(static_cast<ViewID>(VIEW_ID_TAB_0 + i), false);
    107     browser()->OpenURL(GURL(chrome::kAboutBlankURL), GURL(),
    108                        NEW_BACKGROUND_TAB, PageTransition::TYPED);
    109     CheckViewID(static_cast<ViewID>(VIEW_ID_TAB_0 + i), true);
    110     // VIEW_ID_TAB_LAST should always be available.
    111     CheckViewID(VIEW_ID_TAB_LAST, true);
    112   }
    113 
    114   // Open the 11th tab.
    115   browser()->OpenURL(GURL(chrome::kAboutBlankURL), GURL(),
    116                      NEW_BACKGROUND_TAB, PageTransition::TYPED);
    117   CheckViewID(VIEW_ID_TAB_LAST, true);
    118 }
    119