• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 namespace {
12 
WrapSend(const std::string & expression)13 std::string WrapSend(const std::string& expression) {
14   return "window.domAutomationController.send(" + expression + ")";
15 }
16 
17 }  // namespace
18 
19 namespace chromeos {
20 namespace test {
21 
JSChecker()22 JSChecker::JSChecker() : web_contents_(NULL) {}
23 
JSChecker(content::WebContents * web_contents)24 JSChecker::JSChecker(content::WebContents* web_contents)
25     : web_contents_(web_contents) {
26 }
27 
Evaluate(const std::string & expression)28 void JSChecker::Evaluate(const std::string& expression) {
29   CHECK(web_contents_);
30   ASSERT_TRUE(content::ExecuteScript(web_contents_, expression));
31 }
32 
GetBool(const std::string & expression)33 bool JSChecker::GetBool(const std::string& expression) {
34   bool result;
35   GetBoolImpl(expression, &result);
36   return result;
37 }
38 
GetInt(const std::string & expression)39 int JSChecker::GetInt(const std::string& expression) {
40   int result;
41   GetIntImpl(expression, &result);
42   return result;
43 }
44 
GetString(const std::string & expression)45 std::string JSChecker::GetString(const std::string& expression) {
46   std::string result;
47   GetStringImpl(expression, &result);
48   return result;
49 }
50 
ExpectTrue(const std::string & expression)51 void JSChecker::ExpectTrue(const std::string& expression) {
52   EXPECT_TRUE(GetBool(expression)) << expression;
53 }
54 
ExpectFalse(const std::string & expression)55 void JSChecker::ExpectFalse(const std::string& expression) {
56   EXPECT_FALSE(GetBool(expression)) << expression;
57 }
58 
ExpectEQ(const std::string & expression,int result)59 void JSChecker::ExpectEQ(const std::string& expression, int result) {
60   EXPECT_EQ(GetInt(expression), result) << expression;
61 }
62 
ExpectNE(const std::string & expression,int result)63 void JSChecker::ExpectNE(const std::string& expression, int result) {
64   EXPECT_NE(GetInt(expression), result) << expression;
65 }
66 
ExpectEQ(const std::string & expression,const std::string & result)67 void JSChecker::ExpectEQ(const std::string& expression,
68                          const std::string& result) {
69   EXPECT_EQ(GetString(expression), result) << expression;
70 }
71 
ExpectNE(const std::string & expression,const std::string & result)72 void JSChecker::ExpectNE(const std::string& expression,
73                          const std::string& result) {
74   EXPECT_NE(GetString(expression), result) << expression;
75 }
76 
GetBoolImpl(const std::string & expression,bool * result)77 void 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 
GetIntImpl(const std::string & expression,int * result)84 void 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 
GetStringImpl(const std::string & expression,std::string * result)91 void 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