OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/base_switches.h" |
| 6 #include "base/command_line.h" |
| 7 #include "base/test/energy_monitor_mac.h" |
| 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 11 #include "chrome/test/base/in_process_browser_test.h" |
| 12 #include "chrome/test/base/interactive_test_utils.h" |
| 13 #include "content/public/test/web_contents_tester.h" |
| 14 |
| 15 //#include "chrome/test/base/browser_with_content_test.h" |
| 16 |
| 17 const int kWarmupTimeSeconds = 5; |
| 18 const int kNumSamples = 200; // 10 seconds with the default 50ms interval. |
| 19 |
| 20 using base::test::EnergyMonitorMac; |
| 21 |
| 22 // Note these should be run with --single_process passed in to the test harness. |
| 23 // However, the test harness always adds it to the browser command line so |
| 24 // there's no point checking. |
| 25 class EnergyTest : public InProcessBrowserTest { |
| 26 public: |
| 27 EnergyTest() {} |
| 28 |
| 29 void SetUpOnMainThread() override { |
| 30 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 31 |
| 32 TabStripModel* tab_strip = browser()->tab_strip_model(); |
| 33 EXPECT_TRUE(tab_strip); |
| 34 |
| 35 // Note: Real browser_tests shouldn't be using CreateTestWebContents(). |
| 36 web_contents_ = content::WebContentsTester::CreateTestWebContents( |
| 37 browser()->profile(), nullptr); |
| 38 EXPECT_TRUE(web_contents_); |
| 39 |
| 40 tab_strip->InsertWebContentsAt(0, web_contents_, TabStripModel::ADD_ACTIVE); |
| 41 web_contents_tester_ = content::WebContentsTester::For(web_contents_); |
| 42 EXPECT_TRUE(web_contents_tester_); |
| 43 } |
| 44 |
| 45 void PressTab() { |
| 46 // BringBrowserWindowToFront() gives the omnibox focus and flashes the |
| 47 // cursor. To stop the flashing cursor dirtying results, press Tab. |
| 48 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_TAB, false, |
| 49 false, false, false)); |
| 50 } |
| 51 |
| 52 void RunEnergyTest(const char* suffix) { |
| 53 EnergyMonitorMac energy_monitor; |
| 54 energy_monitor.Run(base::TimeDelta::FromSeconds(kWarmupTimeSeconds), |
| 55 kNumSamples); |
| 56 |
| 57 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 58 switches::kTraceToFileName)) { |
| 59 base::FilePath path = |
| 60 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
| 61 switches::kTraceToFileName); |
| 62 |
| 63 path = path.InsertBeforeExtensionASCII(suffix); |
| 64 energy_monitor.WriteTimeSeries(path); // Overwrites. |
| 65 |
| 66 path = path.InsertBeforeExtensionASCII("-log"); |
| 67 std::ostringstream logline; |
| 68 logline << base::Time::Now() << ',' |
| 69 << energy_monitor.GetAverageWatts(EnergyMonitorMac::PROCESSOR) |
| 70 << ',' |
| 71 << energy_monitor.GetStdDevWatts(EnergyMonitorMac::PROCESSOR) |
| 72 << ',' << energy_monitor.GetAverageWatts(EnergyMonitorMac::IA) |
| 73 << ',' << energy_monitor.GetStdDevWatts(EnergyMonitorMac::IA) |
| 74 << '\n'; |
| 75 |
| 76 if (!base::PathExists(path)) { |
| 77 const char header[] = "datetime,processor_watts_avg," |
| 78 "processor_watts_stddev,ia_watts_avg,ia_watts_stddev\n"; |
| 79 base::WriteFile(path, header, strlen(header)); |
| 80 } |
| 81 base::AppendToFile(path, logline.str().data(), logline.str().size()); |
| 82 } |
| 83 |
| 84 VLOG(0) << "\nAverage Processor: " |
| 85 << energy_monitor.GetAverageWatts(EnergyMonitorMac::PROCESSOR) |
| 86 << "\nStdDev Processor: " |
| 87 << energy_monitor.GetStdDevWatts(EnergyMonitorMac::PROCESSOR) |
| 88 << "\nAverage IA: " |
| 89 << energy_monitor.GetAverageWatts(EnergyMonitorMac::IA) |
| 90 << "\nStdDev IA: " |
| 91 << energy_monitor.GetStdDevWatts(EnergyMonitorMac::IA); |
| 92 } |
| 93 |
| 94 protected: |
| 95 content::WebContents* web_contents_ = nullptr; |
| 96 content::WebContentsTester* web_contents_tester_ = nullptr; |
| 97 |
| 98 private: |
| 99 DISALLOW_COPY_AND_ASSIGN(EnergyTest); |
| 100 }; |
| 101 |
| 102 IN_PROC_BROWSER_TEST_F(EnergyTest, Idle) { |
| 103 PressTab(); |
| 104 RunEnergyTest("-idle"); |
| 105 } |
| 106 |
| 107 IN_PROC_BROWSER_TEST_F(EnergyTest, FlashingCursor) { |
| 108 RunEnergyTest("-cursor"); |
| 109 } |
| 110 |
| 111 IN_PROC_BROWSER_TEST_F(EnergyTest, SingleThrobber) { |
| 112 web_contents_tester_->TestSetIsLoading(true); |
| 113 PressTab(); |
| 114 RunEnergyTest("-throbber"); |
| 115 } |
OLD | NEW |