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.
5 #include "chrome/browser/chromeos/login/test/js_checker.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "content/public/browser/render_frame_host.h"
9 #include "content/public/browser/web_contents.h"
10 #include "content/public/test/browser_test_utils.h"
11 #include "content/public/test/test_utils.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 std::string
WrapSend(const std::string
& expression
) {
17 return "window.domAutomationController.send(" + expression
+ ")";
25 JSChecker::JSChecker() : web_contents_(NULL
) {}
27 JSChecker::JSChecker(content::WebContents
* web_contents
)
28 : web_contents_(web_contents
) {
31 void JSChecker::Evaluate(const std::string
& expression
) {
33 ASSERT_TRUE(content::ExecuteScript(web_contents_
, expression
));
36 void JSChecker::ExecuteAsync(const std::string
& expression
) {
38 std::string new_script
= expression
+ ";";
39 web_contents_
->GetMainFrame()->ExecuteJavaScriptWithUserGestureForTests(
40 base::UTF8ToUTF16(new_script
));
43 bool JSChecker::GetBool(const std::string
& expression
) {
45 GetBoolImpl(expression
, &result
);
49 int JSChecker::GetInt(const std::string
& expression
) {
51 GetIntImpl(expression
, &result
);
55 std::string
JSChecker::GetString(const std::string
& expression
) {
57 GetStringImpl(expression
, &result
);
61 void JSChecker::ExpectTrue(const std::string
& expression
) {
62 EXPECT_TRUE(GetBool(expression
)) << expression
;
65 void JSChecker::ExpectFalse(const std::string
& expression
) {
66 EXPECT_FALSE(GetBool(expression
)) << expression
;
69 void JSChecker::ExpectEQ(const std::string
& expression
, int result
) {
70 EXPECT_EQ(GetInt(expression
), result
) << expression
;
73 void JSChecker::ExpectNE(const std::string
& expression
, int result
) {
74 EXPECT_NE(GetInt(expression
), result
) << expression
;
77 void JSChecker::ExpectEQ(const std::string
& expression
,
78 const std::string
& result
) {
79 EXPECT_EQ(GetString(expression
), result
) << expression
;
82 void JSChecker::ExpectNE(const std::string
& expression
,
83 const std::string
& result
) {
84 EXPECT_NE(GetString(expression
), result
) << expression
;
87 void JSChecker::GetBoolImpl(const std::string
& expression
, bool* result
) {
89 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(web_contents_
,
90 "!!" + WrapSend(expression
),
94 void JSChecker::GetIntImpl(const std::string
& expression
, int* result
) {
96 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(web_contents_
,
101 void JSChecker::GetStringImpl(const std::string
& expression
,
102 std::string
* result
) {
103 CHECK(web_contents_
);
104 ASSERT_TRUE(content::ExecuteScriptAndExtractString(web_contents_
,
105 WrapSend(expression
),
110 } // namespace chromeos