| 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 #include "base/macros.h" | 5 #include "base/macros.h" |
| 6 #include "base/strings/stringprintf.h" | 6 #include "base/strings/stringprintf.h" |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/test/base/chrome_render_view_test.h" | 8 #include "chrome/test/base/chrome_render_view_test.h" |
| 9 #include "components/grit/components_resources.h" | 9 #include "components/grit/components_resources.h" |
| 10 #include "components/translate/core/common/translate_errors.h" | 10 #include "components/translate/core/common/translate_errors.h" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 void InjectElementLibrary() { | 81 void InjectElementLibrary() { |
| 82 std::string script; | 82 std::string script; |
| 83 base::StringPiece translate_js = ResourceBundle::GetSharedInstance(). | 83 base::StringPiece translate_js = ResourceBundle::GetSharedInstance(). |
| 84 GetRawDataResource(IDR_TRANSLATE_JS); | 84 GetRawDataResource(IDR_TRANSLATE_JS); |
| 85 translate_js.CopyToString(&script); | 85 translate_js.CopyToString(&script); |
| 86 script += kElementJs; | 86 script += kElementJs; |
| 87 ExecuteScript(script); | 87 ExecuteScript(script); |
| 88 } | 88 } |
| 89 | 89 |
| 90 void ExecuteScript(const std::string& script) { | 90 void ExecuteScript(const std::string& script) { |
| 91 WebScriptSource source = WebScriptSource(base::ASCIIToUTF16(script)); | 91 WebScriptSource source = |
| 92 WebScriptSource(blink::WebString::fromASCII(script)); |
| 92 GetMainFrame()->executeScript(source); | 93 GetMainFrame()->executeScript(source); |
| 93 } | 94 } |
| 94 | 95 |
| 95 bool GetError() { | 96 bool GetError() { |
| 96 return ExecuteScriptAndGetBoolResult(kError); | 97 return ExecuteScriptAndGetBoolResult(kError); |
| 97 } | 98 } |
| 98 | 99 |
| 99 double GetErrorCode() { | 100 double GetErrorCode() { |
| 100 return ExecuteScriptAndGetNumberResult(kErrorCode); | 101 return ExecuteScriptAndGetNumberResult(kErrorCode); |
| 101 } | 102 } |
| 102 | 103 |
| 103 bool IsLibReady() { | 104 bool IsLibReady() { |
| 104 return ExecuteScriptAndGetBoolResult(kLibReady); | 105 return ExecuteScriptAndGetBoolResult(kLibReady); |
| 105 } | 106 } |
| 106 | 107 |
| 107 private: | 108 private: |
| 108 void SetUp() override { ChromeRenderViewTest::SetUp(); } | 109 void SetUp() override { ChromeRenderViewTest::SetUp(); } |
| 109 | 110 |
| 110 void TearDown() override { ChromeRenderViewTest::TearDown(); } | 111 void TearDown() override { ChromeRenderViewTest::TearDown(); } |
| 111 | 112 |
| 112 double ExecuteScriptAndGetNumberResult(const std::string& script) { | 113 double ExecuteScriptAndGetNumberResult(const std::string& script) { |
| 113 WebScriptSource source = WebScriptSource(base::ASCIIToUTF16(script)); | 114 WebScriptSource source = |
| 115 WebScriptSource(blink::WebString::fromASCII(script)); |
| 114 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); | 116 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
| 115 v8::Local<v8::Value> result = | 117 v8::Local<v8::Value> result = |
| 116 GetMainFrame()->executeScriptAndReturnValue(source); | 118 GetMainFrame()->executeScriptAndReturnValue(source); |
| 117 if (result.IsEmpty() || !result->IsNumber()) { | 119 if (result.IsEmpty() || !result->IsNumber()) { |
| 118 NOTREACHED(); | 120 NOTREACHED(); |
| 119 // TODO(toyoshim): Return NaN here and the real implementation in | 121 // TODO(toyoshim): Return NaN here and the real implementation in |
| 120 // TranslateHelper::ExecuteScriptAndGetDoubleResult(). | 122 // TranslateHelper::ExecuteScriptAndGetDoubleResult(). |
| 121 return 0.0; | 123 return 0.0; |
| 122 } | 124 } |
| 123 return result->NumberValue(); | 125 return result->NumberValue(); |
| 124 } | 126 } |
| 125 | 127 |
| 126 bool ExecuteScriptAndGetBoolResult(const std::string& script) { | 128 bool ExecuteScriptAndGetBoolResult(const std::string& script) { |
| 127 WebScriptSource source = WebScriptSource(base::ASCIIToUTF16(script)); | 129 WebScriptSource source = |
| 130 WebScriptSource(blink::WebString::fromASCII(script)); |
| 128 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); | 131 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
| 129 v8::Local<v8::Value> result = | 132 v8::Local<v8::Value> result = |
| 130 GetMainFrame()->executeScriptAndReturnValue(source); | 133 GetMainFrame()->executeScriptAndReturnValue(source); |
| 131 if (result.IsEmpty() || !result->IsBoolean()) { | 134 if (result.IsEmpty() || !result->IsBoolean()) { |
| 132 NOTREACHED(); | 135 NOTREACHED(); |
| 133 return false; | 136 return false; |
| 134 } | 137 } |
| 135 return result->BooleanValue(); | 138 return result->BooleanValue(); |
| 136 } | 139 } |
| 137 | 140 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 EXPECT_FALSE(GetError()); | 233 EXPECT_FALSE(GetError()); |
| 231 EXPECT_EQ(translate::TranslateErrors::NONE, GetErrorCode()); | 234 EXPECT_EQ(translate::TranslateErrors::NONE, GetErrorCode()); |
| 232 | 235 |
| 233 ExecuteScript(kTranslate); | 236 ExecuteScript(kTranslate); |
| 234 | 237 |
| 235 EXPECT_TRUE(GetError()); | 238 EXPECT_TRUE(GetError()); |
| 236 EXPECT_EQ(translate::TranslateErrors::UNSUPPORTED_LANGUAGE, GetErrorCode()); | 239 EXPECT_EQ(translate::TranslateErrors::UNSUPPORTED_LANGUAGE, GetErrorCode()); |
| 237 } | 240 } |
| 238 | 241 |
| 239 // TODO(toyoshim): Add test for onLoadJavaScript. | 242 // TODO(toyoshim): Add test for onLoadJavaScript. |
| OLD | NEW |