| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #import "ios/web/web_state/ui/crw_web_controller.h" | 5 #import "ios/web/web_state/ui/crw_web_controller.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/mac/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #import "ios/testing/ocmock_complex_type_helper.h" | 10 #import "ios/testing/ocmock_complex_type_helper.h" |
| 11 #import "ios/web/public/web_state/crw_web_controller_observer.h" | 11 #import "ios/web/public/web_state/crw_web_controller_observer.h" |
| 12 #import "ios/web/public/web_state/js/crw_js_injection_manager.h" | 12 #import "ios/web/public/web_state/js/crw_js_injection_manager.h" |
| 13 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | 13 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
| 14 #import "ios/web/test/crw_fake_web_controller_observer.h" | 14 #import "ios/web/test/crw_fake_web_controller_observer.h" |
| 15 #import "ios/web/test/web_test.h" | 15 #import "ios/web/test/web_test_with_web_controller.h" |
| 16 #include "testing/gtest_mac.h" | 16 #include "testing/gtest_mac.h" |
| 17 | 17 |
| 18 namespace web { | 18 namespace web { |
| 19 | 19 |
| 20 // Test fixture to test web controller observing. | 20 // Test fixture to test web controller observing. |
| 21 class CRWWebControllerObserverTest : public web::WebTestWithWebController { | 21 class CRWWebControllerObserverTest : public web::WebTestWithWebController { |
| 22 protected: | 22 protected: |
| 23 void SetUp() override { | 23 void SetUp() override { |
| 24 web::WebTestWithWebController::SetUp(); | 24 web::WebTestWithWebController::SetUp(); |
| 25 fake_web_controller_observer_.reset( | 25 fake_web_controller_observer_.reset( |
| 26 [[CRWFakeWebControllerObserver alloc] initWithCommandPrefix:@"test"]); | 26 [[CRWFakeWebControllerObserver alloc] initWithCommandPrefix:@"test"]); |
| 27 [webController_ addObserver:fake_web_controller_observer_]; | 27 [web_controller() addObserver:fake_web_controller_observer_]; |
| 28 } | 28 } |
| 29 | 29 |
| 30 void TearDown() override { | 30 void TearDown() override { |
| 31 [webController_ removeObserver:fake_web_controller_observer_]; | 31 [web_controller() removeObserver:fake_web_controller_observer_]; |
| 32 fake_web_controller_observer_.reset(); | 32 fake_web_controller_observer_.reset(); |
| 33 web::WebTestWithWebController::TearDown(); | 33 web::WebTestWithWebController::TearDown(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 base::scoped_nsobject<CRWFakeWebControllerObserver> | 36 base::scoped_nsobject<CRWFakeWebControllerObserver> |
| 37 fake_web_controller_observer_; | 37 fake_web_controller_observer_; |
| 38 }; | 38 }; |
| 39 | 39 |
| 40 TEST_F(CRWWebControllerObserverTest, PageLoaded) { | 40 TEST_F(CRWWebControllerObserverTest, PageLoaded) { |
| 41 EXPECT_FALSE([fake_web_controller_observer_ pageLoaded]); | 41 EXPECT_FALSE([fake_web_controller_observer_ pageLoaded]); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 56 WaitForBackgroundTasks(); | 56 WaitForBackgroundTasks(); |
| 57 ASSERT_EQ(1U, [fake_web_controller_observer_ commandsReceived].size()); | 57 ASSERT_EQ(1U, [fake_web_controller_observer_ commandsReceived].size()); |
| 58 EXPECT_TRUE( | 58 EXPECT_TRUE( |
| 59 [fake_web_controller_observer_ commandsReceived][0]->Equals(&command)); | 59 [fake_web_controller_observer_ commandsReceived][0]->Equals(&command)); |
| 60 } | 60 } |
| 61 | 61 |
| 62 // Tests that web controller receives an immediate JS message from the page. | 62 // Tests that web controller receives an immediate JS message from the page. |
| 63 TEST_F(CRWWebControllerObserverTest, HandleImmediateCommand) { | 63 TEST_F(CRWWebControllerObserverTest, HandleImmediateCommand) { |
| 64 LoadHtml(@"<p></p>"); | 64 LoadHtml(@"<p></p>"); |
| 65 ASSERT_NSNE(@"http://testimmediate#target", | 65 ASSERT_NSNE(@"http://testimmediate#target", |
| 66 [webController_ externalRequestWindowName]); | 66 [web_controller() externalRequestWindowName]); |
| 67 // The only valid immediate commands are window.unload and externalRequest. | 67 // The only valid immediate commands are window.unload and externalRequest. |
| 68 base::DictionaryValue command; | 68 base::DictionaryValue command; |
| 69 command.SetString("command", "externalRequest"); | 69 command.SetString("command", "externalRequest"); |
| 70 command.SetString("href", "http://testimmediate"); | 70 command.SetString("href", "http://testimmediate"); |
| 71 command.SetString("target", "target"); | 71 command.SetString("target", "target"); |
| 72 command.SetString("referrerPolicy", "referrerPolicy"); | 72 command.SetString("referrerPolicy", "referrerPolicy"); |
| 73 std::string message; | 73 std::string message; |
| 74 base::JSONWriter::Write(command, &message); | 74 base::JSONWriter::Write(command, &message); |
| 75 EvaluateJavaScriptAsString( | 75 EvaluateJavaScriptAsString( |
| 76 [NSString stringWithFormat:@"__gCrWeb.message.invokeOnHostImmediate(%s)", | 76 [NSString stringWithFormat:@"__gCrWeb.message.invokeOnHostImmediate(%s)", |
| 77 message.c_str()]); | 77 message.c_str()]); |
| 78 WaitForBackgroundTasks(); | 78 WaitForBackgroundTasks(); |
| 79 ASSERT_NSEQ(@"http://testimmediate#target", | 79 ASSERT_NSEQ(@"http://testimmediate#target", |
| 80 [webController_ externalRequestWindowName]); | 80 [web_controller() externalRequestWindowName]); |
| 81 } | 81 } |
| 82 | 82 |
| 83 // Send a large number of commands and check each one is immediately received. | 83 // Send a large number of commands and check each one is immediately received. |
| 84 TEST_F(CRWWebControllerObserverTest, HandleMultipleCommands) { | 84 TEST_F(CRWWebControllerObserverTest, HandleMultipleCommands) { |
| 85 LoadHtml(@"<p></p>"); | 85 LoadHtml(@"<p></p>"); |
| 86 | 86 |
| 87 base::DictionaryValue command; | 87 base::DictionaryValue command; |
| 88 command.SetString("command", "test.testMessage"); | 88 command.SetString("command", "test.testMessage"); |
| 89 int kNumberMessages = 200; | 89 int kNumberMessages = 200; |
| 90 for (int count = 0; count <= kNumberMessages; count++) { | 90 for (int count = 0; count <= kNumberMessages; count++) { |
| 91 std::string message; | 91 std::string message; |
| 92 command.SetInteger("number", count); | 92 command.SetInteger("number", count); |
| 93 base::JSONWriter::Write(command, &message); | 93 base::JSONWriter::Write(command, &message); |
| 94 ASSERT_EQ(0U, [fake_web_controller_observer_ commandsReceived].size()); | 94 ASSERT_EQ(0U, [fake_web_controller_observer_ commandsReceived].size()); |
| 95 EvaluateJavaScriptAsString( | 95 EvaluateJavaScriptAsString( |
| 96 [NSString stringWithFormat:@"__gCrWeb.message.invokeOnHost(%s)", | 96 [NSString stringWithFormat:@"__gCrWeb.message.invokeOnHost(%s)", |
| 97 message.c_str()]); | 97 message.c_str()]); |
| 98 WaitForBackgroundTasks(); | 98 WaitForBackgroundTasks(); |
| 99 ASSERT_EQ(1U, [fake_web_controller_observer_ commandsReceived].size()); | 99 ASSERT_EQ(1U, [fake_web_controller_observer_ commandsReceived].size()); |
| 100 EXPECT_TRUE( | 100 EXPECT_TRUE( |
| 101 [fake_web_controller_observer_ commandsReceived][0]->Equals(&command)); | 101 [fake_web_controller_observer_ commandsReceived][0]->Equals(&command)); |
| 102 [fake_web_controller_observer_ commandsReceived].clear(); | 102 [fake_web_controller_observer_ commandsReceived].clear(); |
| 103 ASSERT_EQ(0U, [fake_web_controller_observer_ commandsReceived].size()); | 103 ASSERT_EQ(0U, [fake_web_controller_observer_ commandsReceived].size()); |
| 104 } | 104 } |
| 105 } | 105 } |
| 106 | 106 |
| 107 } // namespace web | 107 } // namespace web |
| OLD | NEW |