OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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_path.h" |
| 7 #include "base/file_util.h" |
| 8 #include "base/path_service.h" |
| 9 #include "base/string_util.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/common/chrome_paths.h" |
| 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/json_value_serializer.h" |
| 14 #include "chrome/test/automation/tab_proxy.h" |
| 15 #include "chrome/test/ui/javascript_test_util.h" |
| 16 #include "chrome/test/ui/ui_test.h" |
| 17 #include "googleurl/src/gurl.h" |
| 18 #include "net/base/net_util.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 static const FilePath::CharType kStartFile[] = |
| 23 FILE_PATH_LITERAL("index.html?dom|jslib&automated"); |
| 24 |
| 25 const wchar_t kRunDromaeo[] = L"run-dromaeo-benchmark"; |
| 26 |
| 27 class DromaeoTest : public UITest { |
| 28 public: |
| 29 typedef std::map<std::string, std::string> ResultsMap; |
| 30 |
| 31 DromaeoTest() : reference_(false) { |
| 32 dom_automation_enabled_ = true; |
| 33 show_window_ = true; |
| 34 } |
| 35 |
| 36 void RunTest() { |
| 37 FilePath::StringType start_file(kStartFile); |
| 38 FilePath test_path = GetDromaeoDir(); |
| 39 test_path = test_path.Append(start_file); |
| 40 GURL test_url(net::FilePathToFileURL(test_path)); |
| 41 |
| 42 scoped_refptr<TabProxy> tab(GetActiveTab()); |
| 43 tab->NavigateToURL(test_url); |
| 44 |
| 45 // Wait for the test to finish. |
| 46 ASSERT_TRUE(WaitUntilTestCompletes(tab.get(), test_url)); |
| 47 |
| 48 PrintResults(tab.get()); |
| 49 } |
| 50 |
| 51 protected: |
| 52 bool reference_; // True if this is a reference build. |
| 53 |
| 54 private: |
| 55 // Return the path to the dromaeo benchmark directory on the local filesystem. |
| 56 FilePath GetDromaeoDir() { |
| 57 FilePath test_dir; |
| 58 PathService::Get(chrome::DIR_TEST_DATA, &test_dir); |
| 59 return test_dir.AppendASCII("dromaeo"); |
| 60 } |
| 61 |
| 62 bool WaitUntilTestCompletes(TabProxy* tab, const GURL& test_url) { |
| 63 return WaitUntilCookieValue(tab, test_url, "__done", 1000, |
| 64 UITest::test_timeout_ms(), "1"); |
| 65 } |
| 66 |
| 67 bool GetScore(TabProxy* tab, std::string* score) { |
| 68 std::wstring score_wide; |
| 69 bool succeeded = tab->ExecuteAndExtractString(L"", |
| 70 L"window.domAutomationController.send(automation.GetScore());", |
| 71 &score_wide); |
| 72 |
| 73 // Note that we don't use ASSERT_TRUE here (and in some other places) as it |
| 74 // doesn't work inside a function with a return type other than void. |
| 75 EXPECT_TRUE(succeeded); |
| 76 if (!succeeded) |
| 77 return false; |
| 78 |
| 79 score->assign(WideToUTF8(score_wide)); |
| 80 return true; |
| 81 } |
| 82 |
| 83 bool GetResults(TabProxy* tab, ResultsMap* results) { |
| 84 std::wstring json_wide; |
| 85 bool succeeded = tab->ExecuteAndExtractString(L"", |
| 86 L"window.domAutomationController.send(" |
| 87 L" JSON.stringify(automation.GetResults()));", |
| 88 &json_wide); |
| 89 |
| 90 EXPECT_TRUE(succeeded); |
| 91 if (!succeeded) |
| 92 return false; |
| 93 |
| 94 std::string json = WideToUTF8(json_wide); |
| 95 return JsonDictionaryToMap(json, results); |
| 96 } |
| 97 |
| 98 void PrintResults(TabProxy* tab) { |
| 99 std::string score; |
| 100 ASSERT_TRUE(GetScore(tab, &score)); |
| 101 |
| 102 ResultsMap results; |
| 103 ASSERT_TRUE(GetResults(tab, &results)); |
| 104 |
| 105 std::string trace_name = reference_ ? "score_ref" : "score"; |
| 106 std::string unit_name = "runs/s"; |
| 107 |
| 108 PrintResult("score", "", trace_name, score, unit_name, true); |
| 109 |
| 110 ResultsMap::const_iterator it = results.begin(); |
| 111 for (; it != results.end(); ++it) |
| 112 PrintResult(it->first, "", trace_name, it->second, unit_name, false); |
| 113 } |
| 114 |
| 115 DISALLOW_COPY_AND_ASSIGN(DromaeoTest); |
| 116 }; |
| 117 |
| 118 class DromaeoReferenceTest : public DromaeoTest { |
| 119 public: |
| 120 DromaeoReferenceTest() : DromaeoTest() { |
| 121 reference_ = true; |
| 122 } |
| 123 |
| 124 // Override the browser directory that is used by UITest::SetUp to cause it |
| 125 // to use the reference build instead. |
| 126 void SetUp() { |
| 127 FilePath dir; |
| 128 PathService::Get(chrome::DIR_TEST_TOOLS, &dir); |
| 129 dir = dir.AppendASCII("reference_build"); |
| 130 #if defined(OS_WIN) |
| 131 dir = dir.AppendASCII("chrome"); |
| 132 #elif defined(OS_LINUX) |
| 133 dir = dir.AppendASCII("chrome_linux"); |
| 134 #elif defined(OS_MACOSX) |
| 135 dir = dir.AppendASCII("chrome_mac"); |
| 136 #endif |
| 137 browser_directory_ = dir; |
| 138 UITest::SetUp(); |
| 139 } |
| 140 }; |
| 141 |
| 142 TEST_F(DromaeoTest, Perf) { |
| 143 if (!CommandLine::ForCurrentProcess()->HasSwitch(kRunDromaeo)) |
| 144 return; |
| 145 |
| 146 RunTest(); |
| 147 } |
| 148 |
| 149 TEST_F(DromaeoReferenceTest, Perf) { |
| 150 if (!CommandLine::ForCurrentProcess()->HasSwitch(kRunDromaeo)) |
| 151 return; |
| 152 |
| 153 RunTest(); |
| 154 } |
| 155 |
| 156 } // namespace |
OLD | NEW |