summaryrefslogtreecommitdiffstats
path: root/chrome/browser/printing/print_preview_tab_controller_unittest.cc
blob: 14ff65be26fcaccbf95390fd8399db1e36239009 (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
// Copyright (c) 2010 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.

#include "chrome/browser/printing/print_preview_tab_controller.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/tabs/tab_strip_model.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/test/browser_with_test_window_test.h"
#include "chrome/test/testing_profile.h"

typedef BrowserWithTestWindowTest PrintPreviewTabControllerTest;

// Create/Get a preview tab for initiator tab.
TEST_F(PrintPreviewTabControllerTest, GetOrCreatePreviewTab) {
  ASSERT_TRUE(browser());
  BrowserList::SetLastActive(browser());
  ASSERT_TRUE(BrowserList::GetLastActive());

  // Lets start with one window with one tab.
  EXPECT_EQ(1u, BrowserList::size());
  EXPECT_EQ(0, browser()->tab_count());
  browser()->NewTab();
  EXPECT_EQ(1, browser()->tab_count());

  // Create a reference to initiator tab contents.
  TabContents* initiator_tab = browser()->GetSelectedTabContents();

  scoped_refptr<printing::PrintPreviewTabController>
      tab_controller(new printing::PrintPreviewTabController());
  ASSERT_TRUE(tab_controller);

  // Get the preview tab for initiator tab.
  TabContents* preview_tab = tab_controller->GetOrCreatePreviewTab(
      initiator_tab, initiator_tab->controller().window_id().id());

  // New print preview tab is created. Current focus is on preview tab.
  EXPECT_EQ(2, browser()->tab_count());
  EXPECT_NE(initiator_tab, preview_tab);

  // Activate initiator_tab.
  initiator_tab->Activate();

  // Get the print preview tab for initiator tab.
  TabContents* new_preview_tab = tab_controller->GetOrCreatePreviewTab(
      initiator_tab, initiator_tab->controller().window_id().id());

  // Preview tab already exists. Tab count remains the same.
  EXPECT_EQ(2, browser()->tab_count());

  // 1:1 relationship between initiator and preview tab.
  EXPECT_EQ(new_preview_tab, preview_tab);
}

// To show multiple print preview tabs exist in the same browser for
// different initiator tabs. If preview tab already exists for an initiator, it
// gets focused.
TEST_F(PrintPreviewTabControllerTest, MultiplePreviewTabs) {
  ASSERT_TRUE(browser());
  BrowserList::SetLastActive(browser());
  ASSERT_TRUE(BrowserList::GetLastActive());

  // Lets start with one window and two tabs.
  EXPECT_EQ(1u, BrowserList::size());
  EXPECT_EQ(0, browser()->tab_count());

  browser()->NewTab();
  TabContents* tab_contents_1 = browser()->GetSelectedTabContents();
  ASSERT_TRUE(tab_contents_1);

  browser()->NewTab();
  TabContents* tab_contents_2 = browser()->GetSelectedTabContents();
  ASSERT_TRUE(tab_contents_2);
  EXPECT_EQ(2, browser()->tab_count());

  scoped_refptr<printing::PrintPreviewTabController>
      tab_controller(new printing::PrintPreviewTabController());
  ASSERT_TRUE(tab_controller);

  // Create preview tab for |tab_contents_1|
  TabContents* preview_tab_1 = tab_controller->GetOrCreatePreviewTab(
      tab_contents_1, tab_contents_1->controller().window_id().id());

  EXPECT_NE(tab_contents_1, preview_tab_1);
  EXPECT_EQ(3, browser()->tab_count());

  // Create preview tab for |tab_contents_2|
  TabContents* preview_tab_2 = tab_controller->GetOrCreatePreviewTab(
      tab_contents_2, tab_contents_2->controller().window_id().id());

  EXPECT_NE(tab_contents_2, preview_tab_2);
  // 2 initiator tab and 2 preview tabs exist in the same browser.
  EXPECT_EQ(4, browser()->tab_count());

  TabStripModel* model = browser()->tabstrip_model();
  ASSERT_TRUE(model);

  int preview_tab_1_index = model->GetWrapperIndex(preview_tab_1);
  int preview_tab_2_index = model->GetWrapperIndex(preview_tab_2);

  EXPECT_NE(-1, preview_tab_1_index);
  EXPECT_NE(-1, preview_tab_2_index);
  // Current tab is |preview_tab_2|.
  EXPECT_EQ(preview_tab_2_index, browser()->selected_index());

  // Activate |tab_contents_1| tab.
  tab_contents_1->Activate();

  // When we get the preview tab for |tab_contents_1|,
  // |preview_tab_1| is activated and focused.
  tab_controller->GetOrCreatePreviewTab(
      tab_contents_1, tab_contents_1->controller().window_id().id());
  EXPECT_EQ(preview_tab_1_index, browser()->selected_index());
}