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