Home | History | Annotate | Download | only in cocoa
      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 #import "chrome/browser/ui/cocoa/tabpose_window.h"
      6 
      7 #include "chrome/browser/tabs/tab_strip_model.h"
      8 #import "chrome/browser/ui/browser_window.h"
      9 #import "chrome/browser/ui/cocoa/browser_test_helper.h"
     10 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
     11 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
     12 #include "content/browser/site_instance.h"
     13 #include "content/browser/tab_contents/tab_contents.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 
     16 class TabposeWindowTest : public CocoaTest {
     17  public:
     18   TabposeWindowTest() {
     19     site_instance_ =
     20         SiteInstance::CreateSiteInstance(browser_helper_.profile());
     21   }
     22 
     23   void AppendTabToStrip() {
     24     TabContentsWrapper* tab_contents = Browser::TabContentsFactory(
     25         browser_helper_.profile(), site_instance_, MSG_ROUTING_NONE,
     26         NULL, NULL);
     27     browser_helper_.browser()->tabstrip_model()->AppendTabContents(
     28         tab_contents, /*foreground=*/true);
     29   }
     30 
     31   BrowserTestHelper browser_helper_;
     32   scoped_refptr<SiteInstance> site_instance_;
     33 };
     34 
     35 // Check that this doesn't leak.
     36 TEST_F(TabposeWindowTest, TestShow) {
     37   BrowserWindow* browser_window = browser_helper_.CreateBrowserWindow();
     38   NSWindow* parent = browser_window->GetNativeHandle();
     39 
     40   [parent orderFront:nil];
     41   EXPECT_TRUE([parent isVisible]);
     42 
     43   // Add a few tabs to the tab strip model.
     44   for (int i = 0; i < 3; ++i)
     45     AppendTabToStrip();
     46 
     47   base::mac::ScopedNSAutoreleasePool pool;
     48   TabposeWindow* window =
     49       [TabposeWindow openTabposeFor:parent
     50                                rect:NSMakeRect(10, 20, 250, 160)
     51                               slomo:NO
     52                      tabStripModel:browser_helper_.browser()->tabstrip_model()];
     53 
     54   // Should release the window.
     55   [window mouseDown:nil];
     56 
     57   browser_helper_.CloseBrowserWindow();
     58 }
     59 
     60 TEST_F(TabposeWindowTest, TestModelObserver) {
     61   BrowserWindow* browser_window = browser_helper_.CreateBrowserWindow();
     62   NSWindow* parent = browser_window->GetNativeHandle();
     63   [parent orderFront:nil];
     64 
     65   // Add a few tabs to the tab strip model.
     66   for (int i = 0; i < 3; ++i)
     67     AppendTabToStrip();
     68 
     69   base::mac::ScopedNSAutoreleasePool pool;
     70   TabposeWindow* window =
     71       [TabposeWindow openTabposeFor:parent
     72                                rect:NSMakeRect(10, 20, 250, 160)
     73                               slomo:NO
     74                      tabStripModel:browser_helper_.browser()->tabstrip_model()];
     75 
     76   // Exercise all the model change events.
     77   TabStripModel* model = browser_helper_.browser()->tabstrip_model();
     78   DCHECK_EQ([window thumbnailLayerCount], 3u);
     79   DCHECK_EQ([window selectedIndex], 2);
     80 
     81   model->MoveTabContentsAt(0, 2, /*select_after_move=*/false);
     82   DCHECK_EQ([window thumbnailLayerCount], 3u);
     83   DCHECK_EQ([window selectedIndex], 1);
     84 
     85   model->MoveTabContentsAt(2, 0, /*select_after_move=*/false);
     86   DCHECK_EQ([window thumbnailLayerCount], 3u);
     87   DCHECK_EQ([window selectedIndex], 2);
     88 
     89   [window selectTileAtIndexWithoutAnimation:0];
     90   DCHECK_EQ([window selectedIndex], 0);
     91 
     92   model->MoveTabContentsAt(0, 2, /*select_after_move=*/false);
     93   DCHECK_EQ([window selectedIndex], 2);
     94 
     95   model->MoveTabContentsAt(2, 0, /*select_after_move=*/false);
     96   DCHECK_EQ([window selectedIndex], 0);
     97 
     98   delete model->DetachTabContentsAt(0);
     99   DCHECK_EQ([window thumbnailLayerCount], 2u);
    100   DCHECK_EQ([window selectedIndex], 0);
    101 
    102   AppendTabToStrip();
    103   DCHECK_EQ([window thumbnailLayerCount], 3u);
    104   DCHECK_EQ([window selectedIndex], 0);
    105 
    106   model->CloseTabContentsAt(0, TabStripModel::CLOSE_NONE);
    107   DCHECK_EQ([window thumbnailLayerCount], 2u);
    108   DCHECK_EQ([window selectedIndex], 0);
    109 
    110   [window selectTileAtIndexWithoutAnimation:1];
    111   model->CloseTabContentsAt(0, TabStripModel::CLOSE_NONE);
    112   DCHECK_EQ([window thumbnailLayerCount], 1u);
    113   DCHECK_EQ([window selectedIndex], 0);
    114 
    115   // Should release the window.
    116   [window mouseDown:nil];
    117 
    118   browser_helper_.CloseBrowserWindow();
    119 }
    120