OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/test/base/web_ui_browsertest.h" | 5 #include "chrome/test/base/web_ui_browser_test.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | |
11 #include "base/bind_helpers.h" | |
12 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
13 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
14 #include "base/path_service.h" | 12 #include "base/path_service.h" |
15 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
17 #include "base/values.h" | 15 #include "base/values.h" |
18 #include "chrome/browser/chrome_content_browser_client.h" | 16 #include "chrome/browser/chrome_content_browser_client.h" |
19 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
20 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
21 #include "chrome/browser/ui/browser_commands.h" | 19 #include "chrome/browser/ui/browser_commands.h" |
22 #include "chrome/browser/ui/browser_navigator.h" | |
23 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
24 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" | |
25 #include "chrome/browser/ui/webui/web_ui_test_handler.h" | 21 #include "chrome/browser/ui/webui/web_ui_test_handler.h" |
26 #include "chrome/common/chrome_paths.h" | 22 #include "chrome/common/chrome_paths.h" |
27 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
28 #include "chrome/test/base/test_chrome_web_ui_controller_factory.h" | 24 #include "chrome/test/base/test_chrome_web_ui_controller_factory.h" |
29 #include "chrome/test/base/ui_test_utils.h" | 25 #include "chrome/test/base/ui_test_utils.h" |
30 #include "content/public/browser/navigation_controller.h" | |
31 #include "content/public/browser/notification_observer.h" | |
32 #include "content/public/browser/notification_registrar.h" | |
33 #include "content/public/browser/notification_service.h" | |
34 #include "content/public/browser/notification_types.h" | |
35 #include "content/public/browser/url_data_source.h" | 26 #include "content/public/browser/url_data_source.h" |
36 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
37 #include "content/public/browser/web_contents_observer.h" | 28 #include "content/public/browser/web_contents_observer.h" |
38 #include "content/public/browser/web_ui_controller.h" | 29 #include "content/public/browser/web_ui_controller.h" |
39 #include "content/public/browser/web_ui_message_handler.h" | 30 #include "content/public/browser/web_ui_message_handler.h" |
40 #include "content/public/test/browser_test_utils.h" | 31 #include "content/public/test/browser_test_utils.h" |
41 #include "content/public/test/test_navigation_observer.h" | 32 #include "content/public/test/test_navigation_observer.h" |
42 #include "net/base/filename_util.h" | 33 #include "net/base/filename_util.h" |
43 #include "testing/gmock/include/gmock/gmock.h" | |
44 #include "testing/gtest/include/gtest/gtest-spi.h" | |
45 #include "ui/base/resource/resource_bundle.h" | 34 #include "ui/base/resource/resource_bundle.h" |
46 #include "ui/base/resource/resource_handle.h" | 35 #include "ui/base/resource/resource_handle.h" |
47 | 36 |
48 #if defined(ENABLE_FULL_PRINTING) | 37 #if defined(ENABLE_FULL_PRINTING) |
49 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 38 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
50 #endif | 39 #endif |
51 | 40 |
52 using content::NavigationController; | |
53 using content::RenderViewHost; | 41 using content::RenderViewHost; |
54 using content::WebContents; | 42 using content::WebContents; |
55 using content::WebUIController; | 43 using content::WebUIController; |
56 using content::WebUIMessageHandler; | 44 using content::WebUIMessageHandler; |
57 | 45 |
58 namespace { | 46 namespace { |
59 | 47 |
60 const base::FilePath::CharType kA11yAuditLibraryJSPath[] = FILE_PATH_LITERAL( | 48 const base::FilePath::CharType kA11yAuditLibraryJSPath[] = FILE_PATH_LITERAL( |
61 "third_party/accessibility-audit/axs_testing.js"); | 49 "third_party/accessibility-audit/axs_testing.js"); |
62 const base::FilePath::CharType kMockJSPath[] = | 50 const base::FilePath::CharType kMockJSPath[] = |
(...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
580 ASSERT_TRUE(web_ui_instance != NULL); | 568 ASSERT_TRUE(web_ui_instance != NULL); |
581 | 569 |
582 test_handler_->set_web_ui(web_ui_instance); | 570 test_handler_->set_web_ui(web_ui_instance); |
583 test_handler_->RegisterMessages(); | 571 test_handler_->RegisterMessages(); |
584 | 572 |
585 if (GetMockMessageHandler()) { | 573 if (GetMockMessageHandler()) { |
586 GetMockMessageHandler()->set_web_ui(web_ui_instance); | 574 GetMockMessageHandler()->set_web_ui(web_ui_instance); |
587 GetMockMessageHandler()->RegisterMessages(); | 575 GetMockMessageHandler()->RegisterMessages(); |
588 } | 576 } |
589 } | 577 } |
590 | |
591 // According to the interface for EXPECT_FATAL_FAILURE | |
592 // (http://code.google.com/p/googletest/wiki/AdvancedGuide#Catching_Failures) | |
593 // the statement must be statically available. Therefore, we make a static | |
594 // global s_test_ which should point to |this| for the duration of the test run | |
595 // and be cleared afterward. | |
596 class WebUIBrowserExpectFailTest : public WebUIBrowserTest { | |
597 public: | |
598 WebUIBrowserExpectFailTest() { | |
599 EXPECT_FALSE(s_test_); | |
600 s_test_ = this; | |
601 } | |
602 | |
603 protected: | |
604 virtual ~WebUIBrowserExpectFailTest() { | |
605 EXPECT_TRUE(s_test_); | |
606 s_test_ = NULL; | |
607 } | |
608 | |
609 static void RunJavascriptTestNoReturn(const std::string& testname) { | |
610 EXPECT_TRUE(s_test_); | |
611 s_test_->RunJavascriptTest(testname); | |
612 } | |
613 | |
614 static void RunJavascriptAsyncTestNoReturn(const std::string& testname) { | |
615 EXPECT_TRUE(s_test_); | |
616 s_test_->RunJavascriptAsyncTest(testname); | |
617 } | |
618 | |
619 private: | |
620 static WebUIBrowserTest* s_test_; | |
621 }; | |
622 | |
623 WebUIBrowserTest* WebUIBrowserExpectFailTest::s_test_ = NULL; | |
624 | |
625 // Test that bogus javascript fails fast - no timeout waiting for result. | |
626 IN_PROC_BROWSER_TEST_F(WebUIBrowserExpectFailTest, TestFailsFast) { | |
627 AddLibrary(base::FilePath(FILE_PATH_LITERAL("sample_downloads.js"))); | |
628 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIDownloadsURL)); | |
629 EXPECT_FATAL_FAILURE(RunJavascriptTestNoReturn("DISABLED_BogusFunctionName"), | |
630 "WebUITestHandler::JavaScriptComplete"); | |
631 } | |
632 | |
633 // Test that bogus javascript fails fast - no timeout waiting for result. | |
634 IN_PROC_BROWSER_TEST_F(WebUIBrowserExpectFailTest, TestRuntimeErrorFailsFast) { | |
635 AddLibrary(base::FilePath(FILE_PATH_LITERAL("runtime_error.js"))); | |
636 ui_test_utils::NavigateToURL(browser(), GURL(kDummyURL)); | |
637 EXPECT_FATAL_FAILURE(RunJavascriptTestNoReturn("TestRuntimeErrorFailsFast"), | |
638 "WebUITestHandler::JavaScriptComplete"); | |
639 } | |
640 | |
641 // Test that bogus javascript fails async test fast as well - no timeout waiting | |
642 // for result. | |
643 IN_PROC_BROWSER_TEST_F(WebUIBrowserExpectFailTest, TestFailsAsyncFast) { | |
644 AddLibrary(base::FilePath(FILE_PATH_LITERAL("sample_downloads.js"))); | |
645 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIDownloadsURL)); | |
646 EXPECT_FATAL_FAILURE( | |
647 RunJavascriptAsyncTestNoReturn("DISABLED_BogusFunctionName"), | |
648 "WebUITestHandler::JavaScriptComplete"); | |
649 } | |
650 | |
651 // Tests that the async framework works. | |
652 class WebUIBrowserAsyncTest : public WebUIBrowserTest { | |
653 public: | |
654 // Calls the testDone() function from test_api.js | |
655 void TestDone() { | |
656 RunJavascriptFunction("testDone"); | |
657 } | |
658 | |
659 // Starts a failing test. | |
660 void RunTestFailsAssert() { | |
661 RunJavascriptFunction("runAsync", new base::StringValue("testFailsAssert")); | |
662 } | |
663 | |
664 // Starts a passing test. | |
665 void RunTestPasses() { | |
666 RunJavascriptFunction("runAsync", new base::StringValue("testPasses")); | |
667 } | |
668 | |
669 protected: | |
670 WebUIBrowserAsyncTest() {} | |
671 | |
672 // Class to synchronize asynchronous javascript activity with the tests. | |
673 class AsyncWebUIMessageHandler : public WebUIMessageHandler { | |
674 public: | |
675 AsyncWebUIMessageHandler() {} | |
676 | |
677 MOCK_METHOD1(HandleTestContinues, void(const base::ListValue*)); | |
678 MOCK_METHOD1(HandleTestFails, void(const base::ListValue*)); | |
679 MOCK_METHOD1(HandleTestPasses, void(const base::ListValue*)); | |
680 | |
681 private: | |
682 virtual void RegisterMessages() OVERRIDE { | |
683 web_ui()->RegisterMessageCallback("startAsyncTest", | |
684 base::Bind(&AsyncWebUIMessageHandler::HandleStartAsyncTest, | |
685 base::Unretained(this))); | |
686 web_ui()->RegisterMessageCallback("testContinues", | |
687 base::Bind(&AsyncWebUIMessageHandler::HandleTestContinues, | |
688 base::Unretained(this))); | |
689 web_ui()->RegisterMessageCallback("testFails", | |
690 base::Bind(&AsyncWebUIMessageHandler::HandleTestFails, | |
691 base::Unretained(this))); | |
692 web_ui()->RegisterMessageCallback("testPasses", | |
693 base::Bind(&AsyncWebUIMessageHandler::HandleTestPasses, | |
694 base::Unretained(this))); | |
695 } | |
696 | |
697 // Starts the test in |list_value|[0] with the runAsync wrapper. | |
698 void HandleStartAsyncTest(const base::ListValue* list_value) { | |
699 const base::Value* test_name; | |
700 ASSERT_TRUE(list_value->Get(0, &test_name)); | |
701 web_ui()->CallJavascriptFunction("runAsync", *test_name); | |
702 } | |
703 | |
704 DISALLOW_COPY_AND_ASSIGN(AsyncWebUIMessageHandler); | |
705 }; | |
706 | |
707 // Handler for this object. | |
708 ::testing::StrictMock<AsyncWebUIMessageHandler> message_handler_; | |
709 | |
710 private: | |
711 // Provide this object's handler. | |
712 virtual WebUIMessageHandler* GetMockMessageHandler() OVERRIDE { | |
713 return &message_handler_; | |
714 } | |
715 | |
716 // Set up and browse to kDummyURL for all tests. | |
717 virtual void SetUpOnMainThread() OVERRIDE { | |
718 WebUIBrowserTest::SetUpOnMainThread(); | |
719 AddLibrary(base::FilePath(FILE_PATH_LITERAL("async.js"))); | |
720 ui_test_utils::NavigateToURL(browser(), GURL(kDummyURL)); | |
721 } | |
722 | |
723 DISALLOW_COPY_AND_ASSIGN(WebUIBrowserAsyncTest); | |
724 }; | |
725 | |
726 // Test that assertions fail immediately after assertion fails (no testContinues | |
727 // message). (Sync version). | |
728 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestSyncOkTestFail) { | |
729 ASSERT_FALSE(RunJavascriptTest("testFailsAssert")); | |
730 } | |
731 | |
732 // Test that assertions fail immediately after assertion fails (no testContinues | |
733 // message). (Async version). | |
734 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestAsyncFailsAssert) { | |
735 EXPECT_CALL(message_handler_, HandleTestFails(::testing::_)); | |
736 ASSERT_FALSE(RunJavascriptAsyncTest( | |
737 "startAsyncTest", new base::StringValue("testFailsAssert"))); | |
738 } | |
739 | |
740 // Test that expectations continue the function, but fail the test. | |
741 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestAsyncFailsExpect) { | |
742 ::testing::InSequence s; | |
743 EXPECT_CALL(message_handler_, HandleTestContinues(::testing::_)); | |
744 EXPECT_CALL(message_handler_, HandleTestFails(::testing::_)); | |
745 ASSERT_FALSE(RunJavascriptAsyncTest( | |
746 "startAsyncTest", new base::StringValue("testFailsExpect"))); | |
747 } | |
748 | |
749 // Test that test continues and passes. (Sync version). | |
750 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestSyncPasses) { | |
751 EXPECT_CALL(message_handler_, HandleTestContinues(::testing::_)); | |
752 ASSERT_TRUE(RunJavascriptTest("testPasses")); | |
753 } | |
754 | |
755 // Test that test continues and passes. (Async version). | |
756 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestAsyncPasses) { | |
757 ::testing::InSequence s; | |
758 EXPECT_CALL(message_handler_, HandleTestContinues(::testing::_)); | |
759 EXPECT_CALL(message_handler_, HandleTestPasses(::testing::_)) | |
760 .WillOnce(::testing::InvokeWithoutArgs( | |
761 this, &WebUIBrowserAsyncTest::TestDone)); | |
762 ASSERT_TRUE(RunJavascriptAsyncTest( | |
763 "startAsyncTest", new base::StringValue("testPasses"))); | |
764 } | |
765 | |
766 // Test that two tests pass. | |
767 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestAsyncPassPass) { | |
768 ::testing::InSequence s; | |
769 EXPECT_CALL(message_handler_, HandleTestContinues(::testing::_)); | |
770 EXPECT_CALL(message_handler_, HandleTestPasses(::testing::_)) | |
771 .WillOnce(::testing::InvokeWithoutArgs( | |
772 this, &WebUIBrowserAsyncTest::RunTestPasses)); | |
773 EXPECT_CALL(message_handler_, HandleTestContinues(::testing::_)); | |
774 EXPECT_CALL(message_handler_, HandleTestPasses(::testing::_)) | |
775 .WillOnce(::testing::InvokeWithoutArgs( | |
776 this, &WebUIBrowserAsyncTest::TestDone)); | |
777 ASSERT_TRUE(RunJavascriptAsyncTest( | |
778 "startAsyncTest", new base::StringValue("testPasses"))); | |
779 } | |
780 | |
781 // Test that first test passes; second fails. | |
782 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestAsyncPassThenFail) { | |
783 ::testing::InSequence s; | |
784 EXPECT_CALL(message_handler_, HandleTestContinues(::testing::_)); | |
785 EXPECT_CALL(message_handler_, HandleTestPasses(::testing::_)) | |
786 .WillOnce(::testing::InvokeWithoutArgs( | |
787 this, &WebUIBrowserAsyncTest::RunTestFailsAssert)); | |
788 EXPECT_CALL(message_handler_, HandleTestFails(::testing::_)); | |
789 ASSERT_FALSE(RunJavascriptAsyncTest( | |
790 "startAsyncTest", new base::StringValue("testPasses"))); | |
791 } | |
792 | |
793 // Test that testDone() with failure first then sync pass still fails. | |
794 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestAsyncDoneFailFirstSyncPass) { | |
795 ::testing::InSequence s; | |
796 EXPECT_CALL(message_handler_, HandleTestContinues(::testing::_)); | |
797 EXPECT_CALL(message_handler_, HandleTestFails(::testing::_)); | |
798 | |
799 // Call runAsync directly instead of deferring through startAsyncTest. It will | |
800 // call testDone() on failure, then return. | |
801 ASSERT_FALSE(RunJavascriptAsyncTest( | |
802 "runAsync", new base::StringValue("testAsyncDoneFailFirstSyncPass"))); | |
803 } | |
804 | |
805 // Test that calling testDone during RunJavascriptAsyncTest still completes | |
806 // when waiting for async result. This is similar to the previous test, but call | |
807 // testDone directly and expect pass result. | |
808 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPassesAsync) { | |
809 ASSERT_TRUE(RunJavascriptAsyncTest("testDone")); | |
810 } | |
811 | |
812 // Test that calling testDone during RunJavascriptTest still completes when | |
813 // waiting for async result. | |
814 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPasses) { | |
815 ASSERT_TRUE(RunJavascriptTest("testDone")); | |
816 } | |
OLD | NEW |