| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "components/translate/ios/browser/js_language_detection_manager.h" | 5 #import "components/translate/ios/browser/js_language_detection_manager.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <memory> |
| 10 #include <vector> |
| 11 |
| 9 #include "base/bind.h" | 12 #include "base/bind.h" |
| 10 #include "base/mac/scoped_nsobject.h" | 13 #include "base/mac/scoped_nsobject.h" |
| 11 #include "base/memory/scoped_vector.h" | |
| 12 #import "base/test/ios/wait_util.h" | 14 #import "base/test/ios/wait_util.h" |
| 13 #include "base/values.h" | 15 #include "base/values.h" |
| 14 #import "ios/chrome/browser/web/chrome_web_test.h" | 16 #import "ios/chrome/browser/web/chrome_web_test.h" |
| 15 #include "ios/chrome/common/string_util.h" | 17 #include "ios/chrome/common/string_util.h" |
| 16 #import "ios/web/public/test/js_test_util.h" | 18 #import "ios/web/public/test/js_test_util.h" |
| 17 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | 19 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
| 18 #import "ios/web/public/web_state/web_state.h" | 20 #import "ios/web/public/web_state/web_state.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "testing/gtest_mac.h" | 22 #include "testing/gtest_mac.h" |
| 21 | 23 |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 web_state()->AddScriptCommandCallback(callback, "languageDetection"); | 267 web_state()->AddScriptCommandCallback(callback, "languageDetection"); |
| 266 } | 268 } |
| 267 void TearDown() override { | 269 void TearDown() override { |
| 268 web_state()->RemoveScriptCommandCallback("languageDetection"); | 270 web_state()->RemoveScriptCommandCallback("languageDetection"); |
| 269 JsLanguageDetectionManagerTest::TearDown(); | 271 JsLanguageDetectionManagerTest::TearDown(); |
| 270 } | 272 } |
| 271 // Called when "languageDetection" command is received. | 273 // Called when "languageDetection" command is received. |
| 272 bool CommandReceived(const base::DictionaryValue& command, | 274 bool CommandReceived(const base::DictionaryValue& command, |
| 273 const GURL&, | 275 const GURL&, |
| 274 bool) { | 276 bool) { |
| 275 commands_received_.push_back(command.DeepCopy()); | 277 commands_received_.push_back(command.CreateDeepCopy()); |
| 276 return true; | 278 return true; |
| 277 } | 279 } |
| 278 | 280 |
| 279 protected: | 281 protected: |
| 280 // Received "languageDetection" commands. | 282 // Received "languageDetection" commands. |
| 281 ScopedVector<base::DictionaryValue> commands_received_; | 283 std::vector<std::unique_ptr<base::DictionaryValue>> commands_received_; |
| 282 }; | 284 }; |
| 283 | 285 |
| 284 // Tests if |__gCrWeb.languageDetection.detectLanguage| correctly informs the | 286 // Tests if |__gCrWeb.languageDetection.detectLanguage| correctly informs the |
| 285 // native side when translation is not allowed. | 287 // native side when translation is not allowed. |
| 286 TEST_F(JsLanguageDetectionManagerDetectLanguageTest, | 288 TEST_F(JsLanguageDetectionManagerDetectLanguageTest, |
| 287 DetectLanguageTranslationNotAllowed) { | 289 DetectLanguageTranslationNotAllowed) { |
| 288 LoadHtmlAndInject(@"<html></html>"); | 290 LoadHtmlAndInject(@"<html></html>"); |
| 289 [manager_ startLanguageDetection]; | 291 [manager_ startLanguageDetection]; |
| 290 // Wait until the original injection has recived a command. | 292 // Wait until the original injection has recived a command. |
| 291 base::test::ios::WaitUntilCondition(^bool() { | 293 base::test::ios::WaitUntilCondition(^bool() { |
| 292 return !commands_received_.empty(); | 294 return !commands_received_.empty(); |
| 293 }); | 295 }); |
| 294 ASSERT_EQ(1U, commands_received_.size()); | 296 ASSERT_EQ(1U, commands_received_.size()); |
| 295 | 297 |
| 296 commands_received_.clear(); | 298 commands_received_.clear(); |
| 297 | 299 |
| 298 // Stub out translationAllowed. | 300 // Stub out translationAllowed. |
| 299 NSString* const kTranslationAllowedJS = | 301 NSString* const kTranslationAllowedJS = |
| 300 @"__gCrWeb.languageDetection.translationAllowed = function() {" | 302 @"__gCrWeb.languageDetection.translationAllowed = function() {" |
| 301 @" return false;" | 303 @" return false;" |
| 302 @"}"; | 304 @"}"; |
| 303 [manager_ executeJavaScript:kTranslationAllowedJS completionHandler:nil]; | 305 [manager_ executeJavaScript:kTranslationAllowedJS completionHandler:nil]; |
| 304 ConditionBlock commands_recieved_block = ^bool { | 306 ConditionBlock commands_recieved_block = ^bool { |
| 305 return commands_received_.size(); | 307 return commands_received_.size(); |
| 306 }; | 308 }; |
| 307 InjectJSAndWaitUntilCondition(@"__gCrWeb.languageDetection.detectLanguage()", | 309 InjectJSAndWaitUntilCondition(@"__gCrWeb.languageDetection.detectLanguage()", |
| 308 commands_recieved_block); | 310 commands_recieved_block); |
| 309 ASSERT_EQ(1U, commands_received_.size()); | 311 ASSERT_EQ(1U, commands_received_.size()); |
| 310 base::DictionaryValue* value = commands_received_[0]; | 312 base::DictionaryValue* value = commands_received_[0].get(); |
| 311 EXPECT_TRUE(value->HasKey("translationAllowed")); | 313 EXPECT_TRUE(value->HasKey("translationAllowed")); |
| 312 bool translation_allowed = true; | 314 bool translation_allowed = true; |
| 313 value->GetBoolean("translationAllowed", &translation_allowed); | 315 value->GetBoolean("translationAllowed", &translation_allowed); |
| 314 EXPECT_FALSE(translation_allowed); | 316 EXPECT_FALSE(translation_allowed); |
| 315 } | 317 } |
| 316 | 318 |
| 317 // Tests if |__gCrWeb.languageDetection.detectLanguage| correctly informs the | 319 // Tests if |__gCrWeb.languageDetection.detectLanguage| correctly informs the |
| 318 // native side when translation is allowed with the right parameters. | 320 // native side when translation is allowed with the right parameters. |
| 319 TEST_F(JsLanguageDetectionManagerDetectLanguageTest, | 321 TEST_F(JsLanguageDetectionManagerDetectLanguageTest, |
| 320 DetectLanguageTranslationAllowed) { | 322 DetectLanguageTranslationAllowed) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 331 ASSERT_EQ(1U, commands_received_.size()); | 333 ASSERT_EQ(1U, commands_received_.size()); |
| 332 | 334 |
| 333 commands_received_.clear(); | 335 commands_received_.clear(); |
| 334 | 336 |
| 335 [manager_ executeJavaScript:@"__gCrWeb.languageDetection.detectLanguage()" | 337 [manager_ executeJavaScript:@"__gCrWeb.languageDetection.detectLanguage()" |
| 336 completionHandler:nil]; | 338 completionHandler:nil]; |
| 337 base::test::ios::WaitUntilCondition(^bool() { | 339 base::test::ios::WaitUntilCondition(^bool() { |
| 338 return !commands_received_.empty(); | 340 return !commands_received_.empty(); |
| 339 }); | 341 }); |
| 340 ASSERT_EQ(1U, commands_received_.size()); | 342 ASSERT_EQ(1U, commands_received_.size()); |
| 341 base::DictionaryValue* value = commands_received_[0]; | 343 base::DictionaryValue* value = commands_received_[0].get(); |
| 342 | 344 |
| 343 EXPECT_TRUE(value->HasKey("translationAllowed")); | 345 EXPECT_TRUE(value->HasKey("translationAllowed")); |
| 344 EXPECT_TRUE(value->HasKey("captureTextTime")); | 346 EXPECT_TRUE(value->HasKey("captureTextTime")); |
| 345 EXPECT_TRUE(value->HasKey("htmlLang")); | 347 EXPECT_TRUE(value->HasKey("htmlLang")); |
| 346 EXPECT_TRUE(value->HasKey("httpContentLanguage")); | 348 EXPECT_TRUE(value->HasKey("httpContentLanguage")); |
| 347 | 349 |
| 348 bool translation_allowed = false; | 350 bool translation_allowed = false; |
| 349 value->GetBoolean("translationAllowed", &translation_allowed); | 351 value->GetBoolean("translationAllowed", &translation_allowed); |
| 350 EXPECT_TRUE(translation_allowed); | 352 EXPECT_TRUE(translation_allowed); |
| 351 } | 353 } |
| OLD | NEW |