1// Copyright 2013 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/ui/views/translate/translate_bubble_view.h"
6
7#include "base/command_line.h"
8#include "base/memory/scoped_ptr.h"
9#include "chrome/browser/chrome_notification_types.h"
10#include "chrome/browser/translate/cld_data_harness.h"
11#include "chrome/browser/ui/browser.h"
12#include "chrome/browser/ui/browser_commands.h"
13#include "chrome/browser/ui/browser_tabstrip.h"
14#include "chrome/browser/ui/tabs/tab_strip_model.h"
15#include "chrome/common/chrome_switches.h"
16#include "chrome/test/base/in_process_browser_test.h"
17#include "chrome/test/base/ui_test_utils.h"
18#include "components/translate/core/common/language_detection_details.h"
19#include "content/public/browser/notification_details.h"
20
21class TranslateBubbleViewBrowserTest : public InProcessBrowserTest {
22 public:
23  TranslateBubbleViewBrowserTest()
24      : cld_data_harness(test::CreateCldDataHarness()) {}
25  virtual ~TranslateBubbleViewBrowserTest() {}
26  virtual void SetUpOnMainThread() OVERRIDE {
27    // We can't Init() until PathService has been initialized. This happens
28    // very late in the test fixture setup process.
29    cld_data_harness->Init();
30    InProcessBrowserTest::SetUpOnMainThread();
31  }
32
33 private:
34  scoped_ptr<test::CldDataHarness> cld_data_harness;
35  DISALLOW_COPY_AND_ASSIGN(TranslateBubbleViewBrowserTest);
36};
37
38// Flaky: crbug.com/394066
39IN_PROC_BROWSER_TEST_F(TranslateBubbleViewBrowserTest,
40                       DISABLED_CloseBrowserWithoutTranslating) {
41  EXPECT_FALSE(TranslateBubbleView::IsShowing());
42
43  // Show a French page and wait until the bubble is shown.
44  content::WebContents* current_web_contents =
45      browser()->tab_strip_model()->GetActiveWebContents();
46  content::Source<content::WebContents> source(current_web_contents);
47  ui_test_utils::WindowedNotificationObserverWithDetails<
48      translate::LanguageDetectionDetails>
49      fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
50                                  source);
51  GURL french_url = ui_test_utils::GetTestUrl(
52      base::FilePath(), base::FilePath(FILE_PATH_LITERAL("french_page.html")));
53  ui_test_utils::NavigateToURL(browser(), french_url);
54  fr_language_detected_signal.Wait();
55  EXPECT_TRUE(TranslateBubbleView::IsShowing());
56
57  // Close the window without translating.
58  chrome::CloseWindow(browser());
59  EXPECT_FALSE(TranslateBubbleView::IsShowing());
60}
61
62// http://crbug.com/378061
63IN_PROC_BROWSER_TEST_F(TranslateBubbleViewBrowserTest,
64                       DISABLED_CloseLastTabWithoutTranslating) {
65  EXPECT_FALSE(TranslateBubbleView::IsShowing());
66
67  // Show a French page and wait until the bubble is shown.
68  content::WebContents* current_web_contents =
69      browser()->tab_strip_model()->GetActiveWebContents();
70  content::Source<content::WebContents> source(current_web_contents);
71  ui_test_utils::WindowedNotificationObserverWithDetails<
72      translate::LanguageDetectionDetails>
73      fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
74                                  source);
75  GURL french_url = ui_test_utils::GetTestUrl(
76      base::FilePath(), base::FilePath(FILE_PATH_LITERAL("french_page.html")));
77  ui_test_utils::NavigateToURL(browser(), french_url);
78  fr_language_detected_signal.Wait();
79  EXPECT_TRUE(TranslateBubbleView::IsShowing());
80
81  // Close the tab without translating.
82  EXPECT_EQ(1, browser()->tab_strip_model()->count());
83  chrome::CloseWebContents(browser(), current_web_contents, false);
84  EXPECT_FALSE(TranslateBubbleView::IsShowing());
85}
86
87IN_PROC_BROWSER_TEST_F(TranslateBubbleViewBrowserTest,
88                       CloseAnotherTabWithoutTranslating) {
89  EXPECT_FALSE(TranslateBubbleView::IsShowing());
90
91  int active_index = browser()->tab_strip_model()->active_index();
92
93  // Open another tab to load a French page on background.
94  int french_index = active_index + 1;
95  GURL french_url = ui_test_utils::GetTestUrl(
96      base::FilePath(), base::FilePath(FILE_PATH_LITERAL("french_page.html")));
97  chrome::AddTabAt(browser(), french_url, french_index, false);
98  EXPECT_EQ(active_index, browser()->tab_strip_model()->active_index());
99  EXPECT_EQ(2, browser()->tab_strip_model()->count());
100
101  // Wait until the language is detected.
102  content::WebContents* web_contents =
103      browser()->tab_strip_model()->GetWebContentsAt(french_index);
104  content::Source<content::WebContents> source(web_contents);
105  ui_test_utils::WindowedNotificationObserverWithDetails<
106      translate::LanguageDetectionDetails>
107      fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
108                                  source);
109  fr_language_detected_signal.Wait();
110
111  // The bubble is not shown because the tab is not activated.
112  EXPECT_FALSE(TranslateBubbleView::IsShowing());
113
114  // Close the French page tab immediately.
115  chrome::CloseWebContents(browser(), web_contents, false);
116  EXPECT_EQ(active_index, browser()->tab_strip_model()->active_index());
117  EXPECT_EQ(1, browser()->tab_strip_model()->count());
118  EXPECT_FALSE(TranslateBubbleView::IsShowing());
119
120  // Close the last tab.
121  chrome::CloseWebContents(browser(),
122                           browser()->tab_strip_model()->GetActiveWebContents(),
123                           false);
124}
125