Home | History | Annotate | Download | only in cocoa
      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 #import "chrome/browser/ui/cocoa/tabpose_window.h"
      6 
      7 #include "base/mac/mac_util.h"
      8 #include "chrome/browser/ui/browser_window.h"
      9 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
     10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     11 #include "content/public/browser/site_instance.h"
     12 #include "content/public/browser/web_contents.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 
     15 using content::SiteInstance;
     16 
     17 class TabposeWindowTest : public CocoaProfileTest {
     18  public:
     19   virtual void SetUp() {
     20     CocoaProfileTest::SetUp();
     21     ASSERT_TRUE(profile());
     22 
     23     site_instance_ = SiteInstance::Create(profile());
     24   }
     25 
     26   void AppendTabToStrip() {
     27     content::WebContents* web_contents = content::WebContents::Create(
     28         content::WebContents::CreateParams(profile(), site_instance_.get()));
     29     browser()->tab_strip_model()->AppendWebContents(
     30         web_contents, /*foreground=*/true);
     31   }
     32 
     33   scoped_refptr<SiteInstance> site_instance_;
     34 };
     35 
     36 TEST_F(TabposeWindowTest, TestShow) {
     37   // Skip this test on 10.7
     38   // http://code.google.com/p/chromium/issues/detail?id=127845
     39   if (base::mac::IsOSLionOrLater()) {
     40     return;
     41   }
     42 
     43   NSWindow* parent = browser()->window()->GetNativeWindow();
     44 
     45   [parent orderFront:nil];
     46   EXPECT_TRUE([parent isVisible]);
     47 
     48   // Add a few tabs to the tab strip model.
     49   for (int i = 0; i < 3; ++i)
     50     AppendTabToStrip();
     51 
     52   base::mac::ScopedNSAutoreleasePool pool;
     53   TabposeWindow* window =
     54       [TabposeWindow openTabposeFor:parent
     55                                rect:NSMakeRect(10, 20, 250, 160)
     56                               slomo:NO
     57                       tabStripModel:browser()->tab_strip_model()];
     58 
     59   // Should release the window.
     60   [window mouseDown:nil];
     61 }
     62 
     63 TEST_F(TabposeWindowTest, TestModelObserver) {
     64   // Skip this test on 10.7
     65   // http://code.google.com/p/chromium/issues/detail?id=127845
     66   if (base::mac::IsOSLionOrLater()) {
     67     return;
     68   }
     69 
     70   NSWindow* parent = browser()->window()->GetNativeWindow();
     71   [parent orderFront:nil];
     72 
     73   // Add a few tabs to the tab strip model.
     74   for (int i = 0; i < 3; ++i)
     75     AppendTabToStrip();
     76 
     77   base::mac::ScopedNSAutoreleasePool pool;
     78   TabposeWindow* window =
     79       [TabposeWindow openTabposeFor:parent
     80                                rect:NSMakeRect(10, 20, 250, 160)
     81                               slomo:NO
     82                       tabStripModel:browser()->tab_strip_model()];
     83 
     84   // Exercise all the model change events.
     85   TabStripModel* model = browser()->tab_strip_model();
     86   DCHECK_EQ([window thumbnailLayerCount], 3u);
     87   DCHECK_EQ([window selectedIndex], 2);
     88 
     89   model->MoveWebContentsAt(0, 2, /*select_after_move=*/false);
     90   DCHECK_EQ([window thumbnailLayerCount], 3u);
     91   DCHECK_EQ([window selectedIndex], 1);
     92 
     93   model->MoveWebContentsAt(2, 0, /*select_after_move=*/false);
     94   DCHECK_EQ([window thumbnailLayerCount], 3u);
     95   DCHECK_EQ([window selectedIndex], 2);
     96 
     97   [window selectTileAtIndexWithoutAnimation:0];
     98   DCHECK_EQ([window selectedIndex], 0);
     99 
    100   model->MoveWebContentsAt(0, 2, /*select_after_move=*/false);
    101   DCHECK_EQ([window selectedIndex], 2);
    102 
    103   model->MoveWebContentsAt(2, 0, /*select_after_move=*/false);
    104   DCHECK_EQ([window selectedIndex], 0);
    105 
    106   delete model->DetachWebContentsAt(0);
    107   DCHECK_EQ([window thumbnailLayerCount], 2u);
    108   DCHECK_EQ([window selectedIndex], 0);
    109 
    110   AppendTabToStrip();
    111   DCHECK_EQ([window thumbnailLayerCount], 3u);
    112   DCHECK_EQ([window selectedIndex], 0);
    113 
    114   model->CloseWebContentsAt(0, TabStripModel::CLOSE_NONE);
    115   DCHECK_EQ([window thumbnailLayerCount], 2u);
    116   DCHECK_EQ([window selectedIndex], 0);
    117 
    118   [window selectTileAtIndexWithoutAnimation:1];
    119   model->CloseWebContentsAt(0, TabStripModel::CLOSE_NONE);
    120   DCHECK_EQ([window thumbnailLayerCount], 1u);
    121   DCHECK_EQ([window selectedIndex], 0);
    122 
    123   // Should release the window.
    124   [window mouseDown:nil];
    125 }
    126