summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/tabpose_window_unittest.mm
blob: 2b8a856c425c2d8daf827c9a44eb74ee6712444b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/ui/cocoa/tabpose_window.h"

#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
#include "chrome/browser/ui/tab_contents/tab_contents.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "content/public/browser/site_instance.h"
#include "ipc/ipc_message.h"
#include "testing/gtest/include/gtest/gtest.h"

using content::SiteInstance;

class TabposeWindowTest : public CocoaProfileTest {
 public:
  virtual void SetUp() {
    CocoaProfileTest::SetUp();
    ASSERT_TRUE(profile());

    site_instance_ = SiteInstance::Create(profile());
  }

  void AppendTabToStrip() {
    TabContents* tab_contents = chrome::TabContentsFactory(
        profile(), site_instance_, MSG_ROUTING_NONE, NULL, NULL);
    browser()->tab_strip_model()->AppendTabContents(
        tab_contents, /*foreground=*/true);
  }

  scoped_refptr<SiteInstance> site_instance_;
};

// Check that this doesn't leak.
TEST_F(TabposeWindowTest, TestShow) {
  BrowserWindow* browser_window = CreateBrowserWindow();
  NSWindow* parent = browser_window->GetNativeWindow();

  [parent orderFront:nil];
  EXPECT_TRUE([parent isVisible]);

  // Add a few tabs to the tab strip model.
  for (int i = 0; i < 3; ++i)
    AppendTabToStrip();

  base::mac::ScopedNSAutoreleasePool pool;
  TabposeWindow* window =
      [TabposeWindow openTabposeFor:parent
                               rect:NSMakeRect(10, 20, 250, 160)
                              slomo:NO
                      tabStripModel:browser()->tab_strip_model()];

  // Should release the window.
  [window mouseDown:nil];
}

TEST_F(TabposeWindowTest, TestModelObserver) {
  BrowserWindow* browser_window = CreateBrowserWindow();
  NSWindow* parent = browser_window->GetNativeWindow();
  [parent orderFront:nil];

  // Add a few tabs to the tab strip model.
  for (int i = 0; i < 3; ++i)
    AppendTabToStrip();

  base::mac::ScopedNSAutoreleasePool pool;
  TabposeWindow* window =
      [TabposeWindow openTabposeFor:parent
                               rect:NSMakeRect(10, 20, 250, 160)
                              slomo:NO
                      tabStripModel:browser()->tab_strip_model()];

  // Exercise all the model change events.
  TabStripModel* model = browser()->tab_strip_model();
  DCHECK_EQ([window thumbnailLayerCount], 3u);
  DCHECK_EQ([window selectedIndex], 2);

  model->MoveTabContentsAt(0, 2, /*select_after_move=*/false);
  DCHECK_EQ([window thumbnailLayerCount], 3u);
  DCHECK_EQ([window selectedIndex], 1);

  model->MoveTabContentsAt(2, 0, /*select_after_move=*/false);
  DCHECK_EQ([window thumbnailLayerCount], 3u);
  DCHECK_EQ([window selectedIndex], 2);

  [window selectTileAtIndexWithoutAnimation:0];
  DCHECK_EQ([window selectedIndex], 0);

  model->MoveTabContentsAt(0, 2, /*select_after_move=*/false);
  DCHECK_EQ([window selectedIndex], 2);

  model->MoveTabContentsAt(2, 0, /*select_after_move=*/false);
  DCHECK_EQ([window selectedIndex], 0);

  delete model->DetachTabContentsAt(0);
  DCHECK_EQ([window thumbnailLayerCount], 2u);
  DCHECK_EQ([window selectedIndex], 0);

  AppendTabToStrip();
  DCHECK_EQ([window thumbnailLayerCount], 3u);
  DCHECK_EQ([window selectedIndex], 0);

  model->CloseTabContentsAt(0, TabStripModel::CLOSE_NONE);
  DCHECK_EQ([window thumbnailLayerCount], 2u);
  DCHECK_EQ([window selectedIndex], 0);

  [window selectTileAtIndexWithoutAnimation:1];
  model->CloseTabContentsAt(0, TabStripModel::CLOSE_NONE);
  DCHECK_EQ([window thumbnailLayerCount], 1u);
  DCHECK_EQ([window selectedIndex], 0);

  // Should release the window.
  [window mouseDown:nil];
}