Home | History | Annotate | Download | only in printing
      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 #include "chrome/browser/printing/print_preview_tab_controller.h"
      6 #include "chrome/browser/tabs/tab_strip_model.h"
      7 #include "chrome/browser/ui/browser_list.h"
      8 #include "chrome/test/browser_with_test_window_test.h"
      9 #include "chrome/test/testing_profile.h"
     10 #include "content/browser/tab_contents/tab_contents.h"
     11 
     12 typedef BrowserWithTestWindowTest PrintPreviewTabControllerTest;
     13 
     14 // Create/Get a preview tab for initiator tab.
     15 TEST_F(PrintPreviewTabControllerTest, GetOrCreatePreviewTab) {
     16   ASSERT_TRUE(browser());
     17   BrowserList::SetLastActive(browser());
     18   ASSERT_TRUE(BrowserList::GetLastActive());
     19 
     20   // Lets start with one window with one tab.
     21   EXPECT_EQ(1u, BrowserList::size());
     22   EXPECT_EQ(0, browser()->tab_count());
     23   browser()->NewTab();
     24   EXPECT_EQ(1, browser()->tab_count());
     25 
     26   // Create a reference to initiator tab contents.
     27   TabContents* initiator_tab = browser()->GetSelectedTabContents();
     28 
     29   scoped_refptr<printing::PrintPreviewTabController>
     30       tab_controller(new printing::PrintPreviewTabController());
     31   ASSERT_TRUE(tab_controller);
     32 
     33   // Get the preview tab for initiator tab.
     34   TabContents* preview_tab =
     35       tab_controller->GetOrCreatePreviewTab(initiator_tab);
     36 
     37   // New print preview tab is created. Current focus is on preview tab.
     38   EXPECT_EQ(2, browser()->tab_count());
     39   EXPECT_NE(initiator_tab, preview_tab);
     40 
     41   // Activate initiator_tab.
     42   initiator_tab->Activate();
     43 
     44   // Get the print preview tab for initiator tab.
     45   TabContents* new_preview_tab =
     46       tab_controller->GetOrCreatePreviewTab(initiator_tab);
     47 
     48   // Preview tab already exists. Tab count remains the same.
     49   EXPECT_EQ(2, browser()->tab_count());
     50 
     51   // 1:1 relationship between initiator and preview tab.
     52   EXPECT_EQ(new_preview_tab, preview_tab);
     53 }
     54 
     55 // To show multiple print preview tabs exist in the same browser for
     56 // different initiator tabs. If preview tab already exists for an initiator, it
     57 // gets focused.
     58 TEST_F(PrintPreviewTabControllerTest, MultiplePreviewTabs) {
     59   ASSERT_TRUE(browser());
     60   BrowserList::SetLastActive(browser());
     61   ASSERT_TRUE(BrowserList::GetLastActive());
     62 
     63   // Lets start with one window and two tabs.
     64   EXPECT_EQ(1u, BrowserList::size());
     65   EXPECT_EQ(0, browser()->tab_count());
     66 
     67   browser()->NewTab();
     68   TabContents* tab_contents_1 = browser()->GetSelectedTabContents();
     69   ASSERT_TRUE(tab_contents_1);
     70 
     71   browser()->NewTab();
     72   TabContents* tab_contents_2 = browser()->GetSelectedTabContents();
     73   ASSERT_TRUE(tab_contents_2);
     74   EXPECT_EQ(2, browser()->tab_count());
     75 
     76   scoped_refptr<printing::PrintPreviewTabController>
     77       tab_controller(new printing::PrintPreviewTabController());
     78   ASSERT_TRUE(tab_controller);
     79 
     80   // Create preview tab for |tab_contents_1|
     81   TabContents* preview_tab_1 =
     82       tab_controller->GetOrCreatePreviewTab(tab_contents_1);
     83 
     84   EXPECT_NE(tab_contents_1, preview_tab_1);
     85   EXPECT_EQ(3, browser()->tab_count());
     86 
     87   // Create preview tab for |tab_contents_2|
     88   TabContents* preview_tab_2 =
     89       tab_controller->GetOrCreatePreviewTab(tab_contents_2);
     90 
     91   EXPECT_NE(tab_contents_2, preview_tab_2);
     92   // 2 initiator tab and 2 preview tabs exist in the same browser.
     93   EXPECT_EQ(4, browser()->tab_count());
     94 
     95   TabStripModel* model = browser()->tabstrip_model();
     96   ASSERT_TRUE(model);
     97 
     98   int preview_tab_1_index = model->GetWrapperIndex(preview_tab_1);
     99   int preview_tab_2_index = model->GetWrapperIndex(preview_tab_2);
    100 
    101   EXPECT_NE(-1, preview_tab_1_index);
    102   EXPECT_NE(-1, preview_tab_2_index);
    103   // Current tab is |preview_tab_2|.
    104   EXPECT_EQ(preview_tab_2_index, browser()->active_index());
    105 
    106   // Activate |tab_contents_1| tab.
    107   tab_contents_1->Activate();
    108 
    109   // When we get the preview tab for |tab_contents_1|,
    110   // |preview_tab_1| is activated and focused.
    111   tab_controller->GetOrCreatePreviewTab(tab_contents_1);
    112   EXPECT_EQ(preview_tab_1_index, browser()->active_index());
    113 }
    114