| 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.AppendASCII(suffix); |
| 64 energy_monitor.WriteTimeSeries(path); // Overwrites. |
| 65 |
| 66 path = path.AppendASCII(".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 |
| 75 base::AppendToFile(path, logline.str().data(), logline.str().size()); |
| 76 } |
| 77 |
| 78 VLOG(0) << "\nAverage Processor: " |
| 79 << energy_monitor.GetAverageWatts(EnergyMonitorMac::PROCESSOR) |
| 80 << "\nStdDev Processor: " |
| 81 << energy_monitor.GetStdDevWatts(EnergyMonitorMac::PROCESSOR) |
| 82 << "\nAverage IA: " |
| 83 << energy_monitor.GetAverageWatts(EnergyMonitorMac::IA) |
| 84 << "\nStdDev IA: " |
| 85 << energy_monitor.GetStdDevWatts(EnergyMonitorMac::IA); |
| 86 } |
| 87 |
| 88 protected: |
| 89 content::WebContents* web_contents_ = nullptr; |
| 90 content::WebContentsTester* web_contents_tester_ = nullptr; |
| 91 |
| 92 private: |
| 93 DISALLOW_COPY_AND_ASSIGN(EnergyTest); |
| 94 }; |
| 95 |
| 96 IN_PROC_BROWSER_TEST_F(EnergyTest, Idle) { |
| 97 PressTab(); |
| 98 RunEnergyTest("-idle"); |
| 99 } |
| 100 |
| 101 IN_PROC_BROWSER_TEST_F(EnergyTest, FlashingCursor) { |
| 102 RunEnergyTest("-cursor"); |
| 103 } |
| 104 |
| 105 IN_PROC_BROWSER_TEST_F(EnergyTest, SingleThrobber) { |
| 106 web_contents_tester_->TestSetIsLoading(true); |
| 107 PressTab(); |
| 108 RunEnergyTest("-throbber"); |
| 109 } |
| OLD | NEW |