| 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/public/web_state/crw_web_controller_observer.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | |
| 8 #include "base/mac/scoped_nsobject.h" | 7 #include "base/mac/scoped_nsobject.h" |
| 9 #include "base/values.h" | |
| 10 #import "ios/testing/ocmock_complex_type_helper.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" | |
| 13 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | |
| 14 #import "ios/web/test/crw_fake_web_controller_observer.h" | 8 #import "ios/web/test/crw_fake_web_controller_observer.h" |
| 15 #import "ios/web/test/web_test_with_web_controller.h" | 9 #import "ios/web/test/web_test_with_web_controller.h" |
| 16 #include "testing/gtest_mac.h" | |
| 17 | 10 |
| 18 namespace web { | 11 namespace web { |
| 19 | 12 |
| 20 // Test fixture to test web controller observing. | 13 // Test fixture to test web controller observing. |
| 21 class CRWWebControllerObserverTest : public web::WebTestWithWebController { | 14 class CRWWebControllerObserverTest : public web::WebTestWithWebController { |
| 22 protected: | 15 protected: |
| 23 void SetUp() override { | 16 void SetUp() override { |
| 24 web::WebTestWithWebController::SetUp(); | 17 web::WebTestWithWebController::SetUp(); |
| 25 fake_web_controller_observer_.reset( | 18 fake_web_controller_observer_.reset( |
| 26 [[CRWFakeWebControllerObserver alloc] initWithCommandPrefix:@"test"]); | 19 [[CRWFakeWebControllerObserver alloc] init]); |
| 27 [web_controller() addObserver:fake_web_controller_observer_]; | 20 [web_controller() addObserver:fake_web_controller_observer_]; |
| 28 } | 21 } |
| 29 | 22 |
| 30 void TearDown() override { | 23 void TearDown() override { |
| 31 [web_controller() removeObserver:fake_web_controller_observer_]; | 24 [web_controller() removeObserver:fake_web_controller_observer_]; |
| 32 fake_web_controller_observer_.reset(); | 25 fake_web_controller_observer_.reset(); |
| 33 web::WebTestWithWebController::TearDown(); | 26 web::WebTestWithWebController::TearDown(); |
| 34 } | 27 } |
| 35 | 28 |
| 36 base::scoped_nsobject<CRWFakeWebControllerObserver> | 29 base::scoped_nsobject<CRWFakeWebControllerObserver> |
| 37 fake_web_controller_observer_; | 30 fake_web_controller_observer_; |
| 38 }; | 31 }; |
| 39 | 32 |
| 40 TEST_F(CRWWebControllerObserverTest, PageLoaded) { | 33 TEST_F(CRWWebControllerObserverTest, PageLoaded) { |
| 41 EXPECT_FALSE([fake_web_controller_observer_ pageLoaded]); | 34 EXPECT_FALSE([fake_web_controller_observer_ pageLoaded]); |
| 42 LoadHtml(@"<p></p>"); | 35 LoadHtml(@"<p></p>"); |
| 43 EXPECT_TRUE([fake_web_controller_observer_ pageLoaded]); | 36 EXPECT_TRUE([fake_web_controller_observer_ pageLoaded]); |
| 44 } | 37 } |
| 45 | 38 |
| 46 // Tests that web controller receives a JS message from the page. | |
| 47 TEST_F(CRWWebControllerObserverTest, HandleCommand) { | |
| 48 LoadHtml(@"<p></p>"); | |
| 49 ASSERT_EQ(0U, [fake_web_controller_observer_ commandsReceived].size()); | |
| 50 base::DictionaryValue command; | |
| 51 command.SetString("command", "test.testMessage"); | |
| 52 std::string message; | |
| 53 base::JSONWriter::Write(command, &message); | |
| 54 ExecuteJavaScript([NSString | |
| 55 stringWithFormat:@"__gCrWeb.message.invokeOnHost(%s)", message.c_str()]); | |
| 56 WaitForBackgroundTasks(); | |
| 57 ASSERT_EQ(1U, [fake_web_controller_observer_ commandsReceived].size()); | |
| 58 EXPECT_TRUE( | |
| 59 [fake_web_controller_observer_ commandsReceived][0]->Equals(&command)); | |
| 60 } | |
| 61 | |
| 62 // Send a large number of commands and check each one is immediately received. | |
| 63 TEST_F(CRWWebControllerObserverTest, HandleMultipleCommands) { | |
| 64 LoadHtml(@"<p></p>"); | |
| 65 | |
| 66 base::DictionaryValue command; | |
| 67 command.SetString("command", "test.testMessage"); | |
| 68 int kNumberMessages = 200; | |
| 69 for (int count = 0; count <= kNumberMessages; count++) { | |
| 70 std::string message; | |
| 71 command.SetInteger("number", count); | |
| 72 base::JSONWriter::Write(command, &message); | |
| 73 ASSERT_EQ(0U, [fake_web_controller_observer_ commandsReceived].size()); | |
| 74 ExecuteJavaScript( | |
| 75 [NSString stringWithFormat:@"__gCrWeb.message.invokeOnHost(%s)", | |
| 76 message.c_str()]); | |
| 77 WaitForBackgroundTasks(); | |
| 78 ASSERT_EQ(1U, [fake_web_controller_observer_ commandsReceived].size()); | |
| 79 EXPECT_TRUE( | |
| 80 [fake_web_controller_observer_ commandsReceived][0]->Equals(&command)); | |
| 81 [fake_web_controller_observer_ commandsReceived].clear(); | |
| 82 ASSERT_EQ(0U, [fake_web_controller_observer_ commandsReceived].size()); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 } // namespace web | 39 } // namespace web |
| OLD | NEW |