| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "app/message_box_flags.h" | 5 #include "app/message_box_flags.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "chrome/browser/renderer_host/render_view_host.h" | 7 #include "chrome/browser/renderer_host/render_view_host.h" |
| 8 #include "chrome/browser/renderer_host/render_widget_host_view.h" | 8 #include "chrome/browser/renderer_host/render_widget_host_view.h" |
| 9 #include "chrome/browser/renderer_host/test/test_render_view_host.h" | 9 #include "chrome/browser/renderer_host/test/test_render_view_host.h" |
| 10 #include "chrome/browser/chrome_thread.h" | 10 #include "chrome/browser/chrome_thread.h" |
| 11 #include "chrome/browser/tab_contents/interstitial_page.h" | 11 #include "chrome/browser/tab_contents/interstitial_page.h" |
| 12 #include "chrome/browser/tab_contents/navigation_controller.h" | 12 #include "chrome/browser/tab_contents/navigation_controller.h" |
| 13 #include "chrome/browser/tab_contents/navigation_entry.h" | 13 #include "chrome/browser/tab_contents/navigation_entry.h" |
| 14 #include "chrome/browser/tab_contents/test_tab_contents.h" | 14 #include "chrome/browser/tab_contents/test_tab_contents.h" |
| 15 #include "chrome/common/chrome_paths.h" | 15 #include "chrome/common/chrome_paths.h" |
| 16 #include "chrome/common/pref_service.h" | 16 #include "chrome/common/pref_service.h" |
| 17 #include "chrome/common/render_messages.h" | 17 #include "chrome/common/render_messages.h" |
| 18 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
| 19 #include "chrome/test/testing_profile.h" | 19 #include "chrome/test/testing_profile.h" |
| 20 #include "chrome/test/ui_test_utils.h" | |
| 21 #include "ipc/ipc_channel.h" | 20 #include "ipc/ipc_channel.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 22 |
| 24 using webkit_glue::PasswordForm; | 23 using webkit_glue::PasswordForm; |
| 25 | 24 |
| 26 static void InitNavigateParams(ViewHostMsg_FrameNavigate_Params* params, | 25 static void InitNavigateParams(ViewHostMsg_FrameNavigate_Params* params, |
| 27 int page_id, | 26 int page_id, |
| 28 const GURL& url) { | 27 const GURL& url) { |
| 29 params->page_id = page_id; | 28 params->page_id = page_id; |
| 30 params->url = url; | 29 params->url = url; |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 } | 187 } |
| 189 | 188 |
| 190 private: | 189 private: |
| 191 TestInterstitialPage* interstitial_page_; | 190 TestInterstitialPage* interstitial_page_; |
| 192 }; | 191 }; |
| 193 | 192 |
| 194 class TabContentsTest : public RenderViewHostTestHarness { | 193 class TabContentsTest : public RenderViewHostTestHarness { |
| 195 public: | 194 public: |
| 196 TabContentsTest() | 195 TabContentsTest() |
| 197 : RenderViewHostTestHarness(), | 196 : RenderViewHostTestHarness(), |
| 198 ui_thread_(ChromeThread::UI, &message_loop_), | 197 ui_thread_(ChromeThread::UI, &message_loop_) { |
| 199 file_thread_(ChromeThread::FILE) { | |
| 200 file_thread_.Start(); | |
| 201 } | 198 } |
| 202 | 199 |
| 203 private: | 200 private: |
| 204 // Supply our own profile so we use the correct profile data. The test harness | 201 // Supply our own profile so we use the correct profile data. The test harness |
| 205 // is not supposed to overwrite a profile if it's already created. | 202 // is not supposed to overwrite a profile if it's already created. |
| 206 virtual void SetUp() { | 203 virtual void SetUp() { |
| 207 profile_.reset(new TabContentsTestingProfile()); | 204 profile_.reset(new TabContentsTestingProfile()); |
| 208 RenderViewHostTestHarness::SetUp(); | 205 RenderViewHostTestHarness::SetUp(); |
| 209 } | 206 } |
| 210 | 207 |
| 211 ChromeThread ui_thread_; | 208 ChromeThread ui_thread_; |
| 212 ChromeThread file_thread_; | |
| 213 }; | 209 }; |
| 214 | 210 |
| 215 // Test to make sure that title updates get stripped of whitespace. | 211 // Test to make sure that title updates get stripped of whitespace. |
| 216 TEST_F(TabContentsTest, UpdateTitle) { | 212 TEST_F(TabContentsTest, UpdateTitle) { |
| 217 ViewHostMsg_FrameNavigate_Params params; | 213 ViewHostMsg_FrameNavigate_Params params; |
| 218 InitNavigateParams(¶ms, 0, GURL(chrome::kAboutBlankURL)); | 214 InitNavigateParams(¶ms, 0, GURL(chrome::kAboutBlankURL)); |
| 219 | 215 |
| 220 NavigationController::LoadCommittedDetails details; | 216 NavigationController::LoadCommittedDetails details; |
| 221 controller().RendererDidNavigate(params, 0, &details); | 217 controller().RendererDidNavigate(params, 0, &details); |
| 222 | 218 |
| (...skipping 1201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1424 | 1420 |
| 1425 // While the interstitial is showing, let's simulate the hidden page | 1421 // While the interstitial is showing, let's simulate the hidden page |
| 1426 // attempting to show a JS message. | 1422 // attempting to show a JS message. |
| 1427 IPC::Message* dummy_message = new IPC::Message; | 1423 IPC::Message* dummy_message = new IPC::Message; |
| 1428 bool did_suppress_message = false; | 1424 bool did_suppress_message = false; |
| 1429 contents()->RunJavaScriptMessage(L"This is an informative message", L"OK", | 1425 contents()->RunJavaScriptMessage(L"This is an informative message", L"OK", |
| 1430 kGURL, MessageBoxFlags::kIsJavascriptAlert, dummy_message, | 1426 kGURL, MessageBoxFlags::kIsJavascriptAlert, dummy_message, |
| 1431 &did_suppress_message); | 1427 &did_suppress_message); |
| 1432 EXPECT_TRUE(did_suppress_message); | 1428 EXPECT_TRUE(did_suppress_message); |
| 1433 } | 1429 } |
| 1434 | |
| 1435 // TODO(port): The compact language detection library works only for Windows. | |
| 1436 #if defined(OS_WIN) | |
| 1437 // Tests the normal case of language detection when a page loads. | |
| 1438 TEST_F(TabContentsTest, NormalLanguageDetection) { | |
| 1439 const GURL kURL("http://www.google.com"); | |
| 1440 const std::wstring kPageContent(L"C'est le contenu de la page. Fantastique!"); | |
| 1441 | |
| 1442 // Navigate to a page. | |
| 1443 NavigateAndCommit(kURL); | |
| 1444 | |
| 1445 NavigationEntry* entry = contents()->controller().GetActiveEntry(); | |
| 1446 ASSERT_TRUE(entry); | |
| 1447 | |
| 1448 // Simulate the PageContents message that triggers the language detection. | |
| 1449 rvh()->TestOnMessageReceived(ViewHostMsg_PageContents(0, | |
| 1450 kURL, entry->page_id(), | |
| 1451 kPageContent)); | |
| 1452 std::string language = ui_test_utils::WaitForLanguageDetection(contents()); | |
| 1453 EXPECT_EQ("fr", language); | |
| 1454 } | |
| 1455 | |
| 1456 // Tests the case of language detection when several page loads happen for the | |
| 1457 // same page. | |
| 1458 TEST_F(TabContentsTest, LanguageDetectionMultipleLoads) { | |
| 1459 const GURL kURL("http://www.google.com"); | |
| 1460 const std::wstring kPageContent(L"The page contents. It is fantastic!"); | |
| 1461 | |
| 1462 // Navigate to a page. | |
| 1463 NavigateAndCommit(kURL); | |
| 1464 | |
| 1465 NavigationEntry* entry = contents()->controller().GetActiveEntry(); | |
| 1466 ASSERT_TRUE(entry); | |
| 1467 | |
| 1468 // Simulate 2 PageContents messages that triggers the language detection. | |
| 1469 rvh()->TestOnMessageReceived(ViewHostMsg_PageContents(0, | |
| 1470 kURL, entry->page_id(), | |
| 1471 kPageContent)); | |
| 1472 rvh()->TestOnMessageReceived(ViewHostMsg_PageContents(0, | |
| 1473 kURL, entry->page_id(), | |
| 1474 kPageContent)); | |
| 1475 std::string language = ui_test_utils::WaitForLanguageDetection(contents()); | |
| 1476 EXPECT_EQ("en", language); | |
| 1477 } | |
| 1478 | |
| 1479 // Tests the case of language detection when several page loads happen. | |
| 1480 TEST_F(TabContentsTest, LanguageDetectionMultipleNavigations) { | |
| 1481 const GURL kURL1("http://www.google.com"); | |
| 1482 const GURL kURL2("http://www.yahoo.com"); | |
| 1483 const std::wstring kPageContent1(L"The page contents. It is fantastic!"); | |
| 1484 const std::wstring kPageContent2(L"C'est le contenu de la page.Fantastique!"); | |
| 1485 // Navigate to the first page. | |
| 1486 NavigateAndCommit(kURL1); | |
| 1487 NavigationEntry* entry = contents()->controller().GetActiveEntry(); | |
| 1488 ASSERT_TRUE(entry); | |
| 1489 | |
| 1490 // Simulate the PageContents message that triggers the language detection. | |
| 1491 rvh()->TestOnMessageReceived(ViewHostMsg_PageContents(0, | |
| 1492 kURL1, entry->page_id(), | |
| 1493 kPageContent1)); | |
| 1494 | |
| 1495 // Before the language notification is back to the UI thread, navigate to a | |
| 1496 // new page. | |
| 1497 NavigateAndCommit(kURL2); | |
| 1498 entry = contents()->controller().GetActiveEntry(); | |
| 1499 ASSERT_TRUE(entry); | |
| 1500 | |
| 1501 // Simulate the PageContents message that triggers the language detection. | |
| 1502 rvh()->TestOnMessageReceived(ViewHostMsg_PageContents(0, | |
| 1503 kURL2, entry->page_id(), | |
| 1504 kPageContent2)); | |
| 1505 | |
| 1506 std::string language = ui_test_utils::WaitForLanguageDetection(contents()); | |
| 1507 EXPECT_EQ("fr", language); | |
| 1508 } | |
| 1509 #endif | |
| 1510 | |
| 1511 // Tests the case of language detection when the tab is closed before the | |
| 1512 // detection has been performed. | |
| 1513 TEST_F(TabContentsTest, LanguageDetectionClosedTab) { | |
| 1514 const GURL kURL("http://www.google.com"); | |
| 1515 const std::wstring kPageContent(L"The page contents. It is fantastic!"); | |
| 1516 // Navigate to the page. | |
| 1517 NavigateAndCommit(kURL); | |
| 1518 NavigationEntry* entry = contents()->controller().GetActiveEntry(); | |
| 1519 ASSERT_TRUE(entry); | |
| 1520 | |
| 1521 // Simulate the PageContents message that triggers the language detection. | |
| 1522 rvh()->TestOnMessageReceived(ViewHostMsg_PageContents(0, | |
| 1523 kURL, entry->page_id(), | |
| 1524 kPageContent)); | |
| 1525 | |
| 1526 // Now close the tab. | |
| 1527 DeleteContents(); | |
| 1528 | |
| 1529 // Run all pending message to make sure nothing crashes. | |
| 1530 MessageLoop::current()->RunAllPending(); | |
| 1531 } | |
| OLD | NEW |