| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 "base/command_line.h" | |
| 6 #include "base/file_util.h" | |
| 7 #include "base/json/json_reader.h" | |
| 8 #include "base/md5.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "base/values.h" | |
| 11 #include "chrome/common/automation_constants.h" | |
| 12 #include "chrome/common/automation_events.h" | |
| 13 #include "chrome/test/base/chrome_render_view_test.h" | |
| 14 #include "chrome/renderer/automation/automation_renderer_helper.h" | |
| 15 #include "content/public/renderer/render_view.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 #include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h" | |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h" | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 const char kThreeBoxesHTML[] = | |
| 23 "<style> * { margin:0px; padding:0px } </style>" | |
| 24 "<body>" | |
| 25 " <div style='background:red;width:100;height:100'></div>" | |
| 26 " <div style='background:green;width:100;height:100'></div>" | |
| 27 " <div style='background:blue;width:100;height:100'></div>" | |
| 28 "</body>"; | |
| 29 | |
| 30 const char kThreeBoxesMD5[] = "3adefecb4472e6ba1812f9af1fdea3e4"; | |
| 31 | |
| 32 void CompareSnapshot(const std::vector<unsigned char>& png_data, | |
| 33 const std::string& reference_md5) { | |
| 34 std::string png_data_str(reinterpret_cast<const char*>(&png_data[0]), | |
| 35 png_data.size()); | |
| 36 if (CommandLine::ForCurrentProcess()->HasSwitch("dump-test-image")) { | |
| 37 base::FilePath path = | |
| 38 base::FilePath().AppendASCII("snapshot" + reference_md5 + ".png"); | |
| 39 EXPECT_EQ(file_util::WriteFile(path, png_data_str.c_str(), png_data.size()), | |
| 40 static_cast<int>(png_data.size())); | |
| 41 } | |
| 42 EXPECT_STREQ(reference_md5.c_str(), base::MD5String(png_data_str).c_str()); | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 typedef ChromeRenderViewTest AutomationRendererHelperTest; | |
| 48 | |
| 49 TEST_F(AutomationRendererHelperTest, BasicSnapshot) { | |
| 50 GetWebWidget()->resize(WebKit::WebSize(100, 300)); | |
| 51 LoadHTML(kThreeBoxesHTML); | |
| 52 std::vector<unsigned char> png_data; | |
| 53 std::string error_msg; | |
| 54 ASSERT_TRUE(AutomationRendererHelper(view_).SnapshotEntirePage( | |
| 55 view_->GetWebView(), &png_data, &error_msg)) << error_msg; | |
| 56 CompareSnapshot(png_data, kThreeBoxesMD5); | |
| 57 } | |
| 58 | |
| 59 TEST_F(AutomationRendererHelperTest, ScrollingSnapshot) { | |
| 60 GetWebWidget()->resize(WebKit::WebSize(40, 90)); | |
| 61 LoadHTML(kThreeBoxesHTML); | |
| 62 std::vector<unsigned char> png_data; | |
| 63 std::string error_msg; | |
| 64 ASSERT_TRUE(AutomationRendererHelper(view_).SnapshotEntirePage( | |
| 65 view_->GetWebView(), &png_data, &error_msg)) << error_msg; | |
| 66 CompareSnapshot(png_data, kThreeBoxesMD5); | |
| 67 } | |
| 68 | |
| 69 TEST_F(AutomationRendererHelperTest, RTLSnapshot) { | |
| 70 GetWebWidget()->resize(WebKit::WebSize(40, 90)); | |
| 71 const char kThreeBoxesRTLHTML[] = | |
| 72 "<style> * { margin:0px; padding:0px } </style>" | |
| 73 "<body dir='rtl'>" | |
| 74 " <div style='background:red;width:100;height:100'></div>" | |
| 75 " <div style='background:green;width:100;height:100'></div>" | |
| 76 " <div style='background:blue;width:100;height:100'></div>" | |
| 77 "</body>"; | |
| 78 LoadHTML(kThreeBoxesRTLHTML); | |
| 79 std::vector<unsigned char> png_data; | |
| 80 std::string error_msg; | |
| 81 ASSERT_TRUE(AutomationRendererHelper(view_).SnapshotEntirePage( | |
| 82 view_->GetWebView(), &png_data, &error_msg)) << error_msg; | |
| 83 CompareSnapshot(png_data, kThreeBoxesMD5); | |
| 84 } | |
| 85 | |
| 86 TEST_F(AutomationRendererHelperTest, ScriptChain) { | |
| 87 ScriptEvaluationRequest request("({'result': 10})", std::string()); | |
| 88 ScriptEvaluationRequest request_plus1("({'result': arguments[0].result + 1})", | |
| 89 std::string()); | |
| 90 std::vector<ScriptEvaluationRequest> script_chain; | |
| 91 script_chain.push_back(request); | |
| 92 script_chain.push_back(request_plus1); | |
| 93 script_chain.push_back(request_plus1); | |
| 94 | |
| 95 AutomationRendererHelper helper(view_); | |
| 96 scoped_ptr<base::DictionaryValue> value; | |
| 97 std::string error; | |
| 98 ASSERT_TRUE(helper.EvaluateScriptChain(script_chain, &value, &error)); | |
| 99 int result; | |
| 100 ASSERT_TRUE(value->GetInteger("result", &result)); | |
| 101 EXPECT_EQ(12, result); | |
| 102 } | |
| 103 | |
| 104 TEST_F(AutomationRendererHelperTest, ScriptChainError) { | |
| 105 ScriptEvaluationRequest request("({'result': 10})", std::string()); | |
| 106 ScriptEvaluationRequest error_request( | |
| 107 "({'result': arguments[0].result + 1, 'error': {'msg': 'some msg'}})", | |
| 108 std::string()); | |
| 109 std::vector<ScriptEvaluationRequest> script_chain; | |
| 110 script_chain.push_back(request); | |
| 111 script_chain.push_back(error_request); | |
| 112 script_chain.push_back(request); | |
| 113 | |
| 114 AutomationRendererHelper helper(view_); | |
| 115 scoped_ptr<base::DictionaryValue> result; | |
| 116 std::string error; | |
| 117 ASSERT_FALSE(helper.EvaluateScriptChain(script_chain, &result, &error)); | |
| 118 scoped_ptr<base::Value> value(base::JSONReader::Read(error)); | |
| 119 base::DictionaryValue* dict; | |
| 120 ASSERT_TRUE(value->GetAsDictionary(&dict)); | |
| 121 std::string msg; | |
| 122 ASSERT_TRUE(dict->GetString("msg", &msg)); | |
| 123 EXPECT_STREQ("some msg", msg.c_str()); | |
| 124 } | |
| OLD | NEW |