Index: chrome/browser/ui/energy_browsertest.cc |
diff --git a/chrome/browser/ui/energy_browsertest.cc b/chrome/browser/ui/energy_browsertest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5e421373ef88af73785012d4d1322086c482927b |
--- /dev/null |
+++ b/chrome/browser/ui/energy_browsertest.cc |
@@ -0,0 +1,115 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/base_switches.h" |
+#include "base/command_line.h" |
+#include "base/test/energy_monitor_mac.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/ui/browser.h" |
+#include "chrome/browser/ui/tabs/tab_strip_model.h" |
+#include "chrome/test/base/in_process_browser_test.h" |
+#include "chrome/test/base/interactive_test_utils.h" |
+#include "content/public/test/web_contents_tester.h" |
+ |
+//#include "chrome/test/base/browser_with_content_test.h" |
+ |
+const int kWarmupTimeSeconds = 5; |
+const int kNumSamples = 200; // 10 seconds with the default 50ms interval. |
+ |
+using base::test::EnergyMonitorMac; |
+ |
+// Note these should be run with --single_process passed in to the test harness. |
+// However, the test harness always adds it to the browser command line so |
+// there's no point checking. |
+class EnergyTest : public InProcessBrowserTest { |
+ public: |
+ EnergyTest() {} |
+ |
+ void SetUpOnMainThread() override { |
+ EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
+ |
+ TabStripModel* tab_strip = browser()->tab_strip_model(); |
+ EXPECT_TRUE(tab_strip); |
+ |
+ // Note: Real browser_tests shouldn't be using CreateTestWebContents(). |
+ web_contents_ = content::WebContentsTester::CreateTestWebContents( |
+ browser()->profile(), nullptr); |
+ EXPECT_TRUE(web_contents_); |
+ |
+ tab_strip->InsertWebContentsAt(0, web_contents_, TabStripModel::ADD_ACTIVE); |
+ web_contents_tester_ = content::WebContentsTester::For(web_contents_); |
+ EXPECT_TRUE(web_contents_tester_); |
+ } |
+ |
+ void PressTab() { |
+ // BringBrowserWindowToFront() gives the omnibox focus and flashes the |
+ // cursor. To stop the flashing cursor dirtying results, press Tab. |
+ EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_TAB, false, |
+ false, false, false)); |
+ } |
+ |
+ void RunEnergyTest(const char* suffix) { |
+ EnergyMonitorMac energy_monitor; |
+ energy_monitor.Run(base::TimeDelta::FromSeconds(kWarmupTimeSeconds), |
+ kNumSamples); |
+ |
+ if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
+ switches::kTraceToFileName)) { |
+ base::FilePath path = |
+ base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
+ switches::kTraceToFileName); |
+ |
+ path = path.InsertBeforeExtensionASCII(suffix); |
+ energy_monitor.WriteTimeSeries(path); // Overwrites. |
+ |
+ path = path.InsertBeforeExtensionASCII("-log"); |
+ std::ostringstream logline; |
+ logline << base::Time::Now() << ',' |
+ << energy_monitor.GetAverageWatts(EnergyMonitorMac::PROCESSOR) |
+ << ',' |
+ << energy_monitor.GetStdDevWatts(EnergyMonitorMac::PROCESSOR) |
+ << ',' << energy_monitor.GetAverageWatts(EnergyMonitorMac::IA) |
+ << ',' << energy_monitor.GetStdDevWatts(EnergyMonitorMac::IA) |
+ << '\n'; |
+ |
+ if (!base::PathExists(path)) { |
+ const char header[] = "datetime,processor_watts_avg," |
+ "processor_watts_stddev,ia_watts_avg,ia_watts_stddev\n"; |
+ base::WriteFile(path, header, strlen(header)); |
+ } |
+ base::AppendToFile(path, logline.str().data(), logline.str().size()); |
+ } |
+ |
+ VLOG(0) << "\nAverage Processor: " |
+ << energy_monitor.GetAverageWatts(EnergyMonitorMac::PROCESSOR) |
+ << "\nStdDev Processor: " |
+ << energy_monitor.GetStdDevWatts(EnergyMonitorMac::PROCESSOR) |
+ << "\nAverage IA: " |
+ << energy_monitor.GetAverageWatts(EnergyMonitorMac::IA) |
+ << "\nStdDev IA: " |
+ << energy_monitor.GetStdDevWatts(EnergyMonitorMac::IA); |
+ } |
+ |
+ protected: |
+ content::WebContents* web_contents_ = nullptr; |
+ content::WebContentsTester* web_contents_tester_ = nullptr; |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(EnergyTest); |
+}; |
+ |
+IN_PROC_BROWSER_TEST_F(EnergyTest, Idle) { |
+ PressTab(); |
+ RunEnergyTest("-idle"); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(EnergyTest, FlashingCursor) { |
+ RunEnergyTest("-cursor"); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(EnergyTest, SingleThrobber) { |
+ web_contents_tester_->TestSetIsLoading(true); |
+ PressTab(); |
+ RunEnergyTest("-throbber"); |
+} |