js_checker.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 3// found in the LICENSE file. 4 5#include "chrome/browser/chromeos/login/test/js_checker.h" 6 7#include "content/public/test/browser_test_utils.h" 8#include "content/public/test/test_utils.h" 9#include "testing/gtest/include/gtest/gtest.h" 10 11namespace { 12 13std::string WrapSend(const std::string& expression) { 14 return "window.domAutomationController.send(" + expression + ")"; 15} 16 17} // namespace 18 19namespace chromeos { 20namespace test { 21 22JSChecker::JSChecker() : web_contents_(NULL) {} 23 24JSChecker::JSChecker(content::WebContents* web_contents) 25 : web_contents_(web_contents) { 26} 27 28void JSChecker::Evaluate(const std::string& expression) { 29 CHECK(web_contents_); 30 ASSERT_TRUE(content::ExecuteScript(web_contents_, expression)); 31} 32 33bool JSChecker::GetBool(const std::string& expression) { 34 bool result; 35 GetBoolImpl(expression, &result); 36 return result; 37} 38 39int JSChecker::GetInt(const std::string& expression) { 40 int result; 41 GetIntImpl(expression, &result); 42 return result; 43} 44 45std::string JSChecker::GetString(const std::string& expression) { 46 std::string result; 47 GetStringImpl(expression, &result); 48 return result; 49} 50 51void JSChecker::ExpectTrue(const std::string& expression) { 52 EXPECT_TRUE(GetBool(expression)) << expression; 53} 54 55void JSChecker::ExpectFalse(const std::string& expression) { 56 EXPECT_FALSE(GetBool(expression)) << expression; 57} 58 59void JSChecker::ExpectEQ(const std::string& expression, int result) { 60 EXPECT_EQ(GetInt(expression), result) << expression; 61} 62 63void JSChecker::ExpectNE(const std::string& expression, int result) { 64 EXPECT_NE(GetInt(expression), result) << expression; 65} 66 67void JSChecker::ExpectEQ(const std::string& expression, 68 const std::string& result) { 69 EXPECT_EQ(GetString(expression), result) << expression; 70} 71 72void JSChecker::ExpectNE(const std::string& expression, 73 const std::string& result) { 74 EXPECT_NE(GetString(expression), result) << expression; 75} 76 77void JSChecker::GetBoolImpl(const std::string& expression, bool* result) { 78 CHECK(web_contents_); 79 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(web_contents_, 80 "!!" + WrapSend(expression), 81 result)); 82} 83 84void JSChecker::GetIntImpl(const std::string& expression, int* result) { 85 CHECK(web_contents_); 86 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(web_contents_, 87 WrapSend(expression), 88 result)); 89} 90 91void JSChecker::GetStringImpl(const std::string& expression, 92 std::string* result) { 93 CHECK(web_contents_); 94 ASSERT_TRUE(content::ExecuteScriptAndExtractString(web_contents_, 95 WrapSend(expression), 96 result)); 97} 98 99} // namespace test 100} // namespace chromeos 101