Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / chromeos / login / test / js_checker.cc
blob0b5db2c7dde8e6c6d3e850c6eae7eb726b645044
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"
14 namespace {
16 std::string WrapSend(const std::string& expression) {
17 return "window.domAutomationController.send(" + expression + ")";
20 } // namespace
22 namespace chromeos {
23 namespace test {
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) {
32 CHECK(web_contents_);
33 ASSERT_TRUE(content::ExecuteScript(web_contents_, expression));
36 void JSChecker::ExecuteAsync(const std::string& expression) {
37 CHECK(web_contents_);
38 std::string new_script = expression + ";";
39 web_contents_->GetMainFrame()->ExecuteJavaScriptWithUserGestureForTests(
40 base::UTF8ToUTF16(new_script));
43 bool JSChecker::GetBool(const std::string& expression) {
44 bool result;
45 GetBoolImpl(expression, &result);
46 return result;
49 int JSChecker::GetInt(const std::string& expression) {
50 int result;
51 GetIntImpl(expression, &result);
52 return result;
55 std::string JSChecker::GetString(const std::string& expression) {
56 std::string result;
57 GetStringImpl(expression, &result);
58 return 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) {
88 CHECK(web_contents_);
89 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(web_contents_,
90 "!!" + WrapSend(expression),
91 result));
94 void JSChecker::GetIntImpl(const std::string& expression, int* result) {
95 CHECK(web_contents_);
96 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(web_contents_,
97 WrapSend(expression),
98 result));
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),
106 result));
109 } // namespace test
110 } // namespace chromeos