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.
5 #include "chrome/browser/ui/views/translate/translate_bubble_view.h"
7 #include "base/command_line.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_commands.h"
11 #include "chrome/browser/ui/browser_tabstrip.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "chrome/test/base/ui_test_utils.h"
16 #include "components/translate/core/common/language_detection_details.h"
17 #include "content/public/browser/notification_details.h"
19 class TranslateBubbleViewBrowserTest
: public InProcessBrowserTest
{
21 TranslateBubbleViewBrowserTest() {}
22 virtual ~TranslateBubbleViewBrowserTest() {}
25 DISALLOW_COPY_AND_ASSIGN(TranslateBubbleViewBrowserTest
);
28 IN_PROC_BROWSER_TEST_F(TranslateBubbleViewBrowserTest
,
29 CloseBrowserWithoutTranslating
) {
30 EXPECT_FALSE(TranslateBubbleView::IsShowing());
32 // Show a French page and wait until the bubble is shown.
33 content::WebContents
* current_web_contents
=
34 browser()->tab_strip_model()->GetActiveWebContents();
35 content::Source
<content::WebContents
> source(current_web_contents
);
36 ui_test_utils::WindowedNotificationObserverWithDetails
<
37 LanguageDetectionDetails
>
38 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED
,
40 GURL french_url
= ui_test_utils::GetTestUrl(
41 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("french_page.html")));
42 ui_test_utils::NavigateToURL(browser(), french_url
);
43 fr_language_detected_signal
.Wait();
44 EXPECT_TRUE(TranslateBubbleView::IsShowing());
46 // Close the window without translating.
47 chrome::CloseWindow(browser());
48 EXPECT_FALSE(TranslateBubbleView::IsShowing());
51 IN_PROC_BROWSER_TEST_F(TranslateBubbleViewBrowserTest
,
52 CloseLastTabWithoutTranslating
) {
53 EXPECT_FALSE(TranslateBubbleView::IsShowing());
55 // Show a French page and wait until the bubble is shown.
56 content::WebContents
* current_web_contents
=
57 browser()->tab_strip_model()->GetActiveWebContents();
58 content::Source
<content::WebContents
> source(current_web_contents
);
59 ui_test_utils::WindowedNotificationObserverWithDetails
<
60 LanguageDetectionDetails
>
61 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED
,
63 GURL french_url
= ui_test_utils::GetTestUrl(
64 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("french_page.html")));
65 ui_test_utils::NavigateToURL(browser(), french_url
);
66 fr_language_detected_signal
.Wait();
67 EXPECT_TRUE(TranslateBubbleView::IsShowing());
69 // Close the tab without translating.
70 EXPECT_EQ(1, browser()->tab_strip_model()->count());
71 chrome::CloseWebContents(browser(), current_web_contents
, false);
72 EXPECT_FALSE(TranslateBubbleView::IsShowing());
75 IN_PROC_BROWSER_TEST_F(TranslateBubbleViewBrowserTest
,
76 CloseAnotherTabWithoutTranslating
) {
77 EXPECT_FALSE(TranslateBubbleView::IsShowing());
79 int active_index
= browser()->tab_strip_model()->active_index();
81 // Open another tab to load a French page on background.
82 int french_index
= active_index
+ 1;
83 GURL french_url
= ui_test_utils::GetTestUrl(
84 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("french_page.html")));
85 chrome::AddTabAt(browser(), french_url
, french_index
, false);
86 EXPECT_EQ(active_index
, browser()->tab_strip_model()->active_index());
87 EXPECT_EQ(2, browser()->tab_strip_model()->count());
89 // Wait until the language is detected.
90 content::WebContents
* web_contents
=
91 browser()->tab_strip_model()->GetWebContentsAt(french_index
);
92 content::Source
<content::WebContents
> source(web_contents
);
93 ui_test_utils::WindowedNotificationObserverWithDetails
<
94 LanguageDetectionDetails
>
95 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED
,
97 fr_language_detected_signal
.Wait();
99 // The bubble is not shown because the tab is not activated.
100 EXPECT_FALSE(TranslateBubbleView::IsShowing());
102 // Close the French page tab immediately.
103 chrome::CloseWebContents(browser(), web_contents
, false);
104 EXPECT_EQ(active_index
, browser()->tab_strip_model()->active_index());
105 EXPECT_EQ(1, browser()->tab_strip_model()->count());
106 EXPECT_FALSE(TranslateBubbleView::IsShowing());
108 // Close the last tab.
109 chrome::CloseWebContents(browser(),
110 browser()->tab_strip_model()->GetActiveWebContents(),