Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / translate / ios / browser / language_detection_controller_unittest.mm
blobe63163e37037a72ba11152648105bd71ecd8286c
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
3 // found in the LICENSE file.
5 #import "components/translate/ios/browser/language_detection_controller.h"
7 #include "base/mac/bind_objc_block.h"
8 #include "base/prefs/pref_registry_simple.h"
9 #include "base/prefs/testing_pref_service.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/translate/core/common/translate_pref_names.h"
12 #import "components/translate/ios/browser/js_language_detection_manager.h"
13 #include "ios/web/public/test/test_web_state.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/platform_test.h"
17 @interface MockJsLanguageDetectionManager : JsLanguageDetectionManager
18 @end
20 @implementation MockJsLanguageDetectionManager
21 - (void)retrieveBufferedTextContent:
22         (const language_detection::BufferedTextCallback&)callback {
23   callback.Run(base::UTF8ToUTF16("Some content"));
25 @end
27 namespace translate {
29 namespace {
31 class LanguageDetectionControllerTest : public PlatformTest {
32  protected:
33   LanguageDetectionControllerTest() {
34     prefs_.registry()->RegisterBooleanPref(prefs::kEnableTranslate, true);
36     base::scoped_nsobject<MockJsLanguageDetectionManager> js_manager(
37         [[MockJsLanguageDetectionManager alloc] init]);
38     controller_.reset(new LanguageDetectionController(
39         &web_state_, js_manager.get(), &prefs_));
40   }
42   LanguageDetectionController* controller() { return controller_.get(); }
44  private:
45   TestingPrefServiceSimple prefs_;
46   web::TestWebState web_state_;
47   scoped_ptr<LanguageDetectionController> controller_;
50 }  // namespace
52 // Tests that OnTextCaptured() correctly handles messages from the JS side and
53 // informs the driver.
54 TEST_F(LanguageDetectionControllerTest, OnTextCaptured) {
55   const std::string kRootLanguage("en");
56   const std::string kContentLanguage("fr");
58   __block bool block_was_called = false;
59   auto subscription =
60       controller()->RegisterLanguageDetectionCallback(base::BindBlock(
61           ^(const LanguageDetectionController::DetectionDetails& details) {
62             block_was_called = true;
63             EXPECT_EQ(kRootLanguage, details.html_root_language);
64             EXPECT_EQ(kContentLanguage, details.content_language);
65           }));
67   base::DictionaryValue command;
68   command.SetString("command", "languageDetection.textCaptured");
69   command.SetBoolean("translationAllowed", true);
70   command.SetInteger("captureTextTime", 10);
71   command.SetString("htmlLang", kRootLanguage);
72   command.SetString("httpContentLanguage", kContentLanguage);
73   controller()->OnTextCaptured(command, GURL("http://google.com"), false);
75   EXPECT_TRUE(block_was_called);
78 }  // namespace translate