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/path_service.h"
6#include "base/strings/stringprintf.h"
7#include "base/time/time.h"
8#include "chrome/app/chrome_command_ids.h"
9#include "chrome/common/chrome_paths.h"
10#include "chrome/common/chrome_switches.h"
11#include "chrome/test/automation/automation_proxy.h"
12#include "chrome/test/automation/browser_proxy.h"
13#include "chrome/test/automation/window_proxy.h"
14#include "chrome/test/perf/perf_test.h"
15#include "chrome/test/perf/perf_ui_test_suite.h"
16#include "chrome/test/ui/ui_perf_test.h"
17#include "net/base/net_util.h"
18#include "testing/perf/perf_test.h"
19#include "ui/gfx/rect.h"
20
21using base::TimeDelta;
22
23namespace {
24
25class NewTabUIStartupTest : public UIPerfTest {
26 public:
27  NewTabUIStartupTest() {
28    show_window_ = true;
29  }
30
31  virtual void SetUp() {}
32  virtual void TearDown() {}
33
34  static const int kNumCycles = 5;
35
36  void PrintTimings(const char* label, TimeDelta timings[kNumCycles],
37                    bool important) {
38    std::string times;
39    for (int i = 0; i < kNumCycles; ++i)
40      base::StringAppendF(&times, "%.2f,", timings[i].InMillisecondsF());
41    perf_test::PrintResultList(
42        "new_tab", std::string(), label, times, "ms", important);
43  }
44
45  void InitProfile(PerfUITestSuite::ProfileType profile_type) {
46    // Install the location of the test profile file.
47    set_template_user_data(
48        PerfUITestSuite::GetPathForProfileType(profile_type));
49  }
50
51  // Run the test, by bringing up a browser and timing the new tab startup.
52  // |want_warm| is true if we should output warm-disk timings, false if
53  // we should report cold timings.
54  void RunStartupTest(const char* label, bool want_warm, bool important,
55                      PerfUITestSuite::ProfileType profile_type) {
56    InitProfile(profile_type);
57
58    TimeDelta timings[kNumCycles];
59    for (int i = 0; i < kNumCycles; ++i) {
60      UITest::SetUp();
61
62      // Switch to the "new tab" tab, which should be any new tab after the
63      // first (the first is about:blank).
64      scoped_refptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
65      ASSERT_TRUE(window.get());
66
67      // We resize the window so that we hit the normal layout of the NTP and
68      // not the small layout mode.
69#if defined(OS_WIN)
70      // TODO(port): SetBounds returns false when not implemented.
71      // It is OK to comment out the resize since it will still be useful to
72      // test the default size of the window.
73      ASSERT_TRUE(window->GetWindow().get()->SetBounds(gfx::Rect(1000, 1000)));
74#endif
75      int tab_count = -1;
76      ASSERT_TRUE(window->GetTabCount(&tab_count));
77      ASSERT_EQ(1, tab_count);
78
79      // Hit ctl-t and wait for the tab to load.
80      ASSERT_TRUE(window->RunCommand(IDC_NEW_TAB));
81      ASSERT_TRUE(window->GetTabCount(&tab_count));
82      ASSERT_EQ(2, tab_count);
83      int load_time;
84      ASSERT_TRUE(automation()->WaitForInitialNewTabUILoad(&load_time));
85
86      if (want_warm) {
87        // Bring up a second tab, now that we've already shown one tab.
88        ASSERT_TRUE(window->RunCommand(IDC_NEW_TAB));
89        ASSERT_TRUE(window->GetTabCount(&tab_count));
90        ASSERT_EQ(3, tab_count);
91        ASSERT_TRUE(automation()->WaitForInitialNewTabUILoad(&load_time));
92      }
93      timings[i] = TimeDelta::FromMilliseconds(load_time);
94
95      window = NULL;
96      UITest::TearDown();
97    }
98
99    PrintTimings(label, timings, important);
100  }
101
102  void RunNewTabTimingTest() {
103    InitProfile(PerfUITestSuite::DEFAULT_THEME);
104
105    TimeDelta scriptstart_times[kNumCycles];
106    TimeDelta domcontentloaded_times[kNumCycles];
107    TimeDelta onload_times[kNumCycles];
108
109    for (int i = 0; i < kNumCycles; ++i) {
110      UITest::SetUp();
111
112      // Switch to the "new tab" tab, which should be any new tab after the
113      // first (the first is about:blank).
114      scoped_refptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
115      ASSERT_TRUE(window.get());
116
117      // We resize the window so that we hit the normal layout of the NTP and
118      // not the small layout mode.
119#if defined(OS_WIN)
120      // TODO(port): SetBounds returns false when not implemented.
121      // It is OK to comment out the resize since it will still be useful to
122      // test the default size of the window.
123      ASSERT_TRUE(window->GetWindow().get()->SetBounds(gfx::Rect(1000, 1000)));
124#endif
125      int tab_count = -1;
126      ASSERT_TRUE(window->GetTabCount(&tab_count));
127      ASSERT_EQ(1, tab_count);
128
129      // Hit ctl-t and wait for the tab to load.
130      ASSERT_TRUE(window->RunCommand(IDC_NEW_TAB));
131      ASSERT_TRUE(window->GetTabCount(&tab_count));
132      ASSERT_EQ(2, tab_count);
133      int duration;
134      ASSERT_TRUE(automation()->WaitForInitialNewTabUILoad(&duration));
135
136      // Collect the timing information.
137      ASSERT_TRUE(automation()->GetMetricEventDuration("Tab.NewTabScriptStart",
138          &duration));
139      scriptstart_times[i] = TimeDelta::FromMilliseconds(duration);
140
141      ASSERT_TRUE(automation()->GetMetricEventDuration(
142          "Tab.NewTabDOMContentLoaded", &duration));
143      domcontentloaded_times[i] = TimeDelta::FromMilliseconds(duration);
144
145      ASSERT_TRUE(automation()->GetMetricEventDuration("Tab.NewTabOnload",
146          &duration));
147      onload_times[i] = TimeDelta::FromMilliseconds(duration);
148
149      window = NULL;
150      UITest::TearDown();
151    }
152
153    PrintTimings("script_start", scriptstart_times, false /* important */);
154    PrintTimings("domcontent_loaded", domcontentloaded_times,
155                 false /* important */);
156    PrintTimings("onload", onload_times, false /* important */);
157  }
158};
159
160// FLAKY: http://crbug.com/69940
161TEST_F(NewTabUIStartupTest, DISABLED_PerfRefCold) {
162  UseReferenceBuild();
163  RunStartupTest("tab_cold_ref", false /* cold */, true /* important */,
164                 PerfUITestSuite::DEFAULT_THEME);
165}
166
167// FLAKY: http://crbug.com/69940
168TEST_F(NewTabUIStartupTest, DISABLED_PerfCold) {
169  RunStartupTest("tab_cold", false /* cold */, true /* important */,
170                 PerfUITestSuite::DEFAULT_THEME);
171}
172
173// FLAKY: http://crbug.com/69940
174TEST_F(NewTabUIStartupTest, DISABLED_PerfRefWarm) {
175  UseReferenceBuild();
176  RunStartupTest("tab_warm_ref", true /* warm */, true /* not important */,
177                 PerfUITestSuite::DEFAULT_THEME);
178}
179
180// FLAKY: http://crbug.com/69940
181TEST_F(NewTabUIStartupTest, DISABLED_PerfWarm) {
182  RunStartupTest("tab_warm", true /* warm */, true /* not important */,
183                 PerfUITestSuite::DEFAULT_THEME);
184}
185
186// FLAKY: http://crbug.com/69940
187TEST_F(NewTabUIStartupTest, DISABLED_ComplexThemeCold) {
188  RunStartupTest("tab_complex_theme_cold", false /* cold */,
189                 false /* not important */,
190                 PerfUITestSuite::COMPLEX_THEME);
191}
192
193// FLAKY: http://crbug.com/69940
194TEST_F(NewTabUIStartupTest, DISABLED_NewTabTimingTestsCold) {
195  RunNewTabTimingTest();
196}
197
198}  // namespace
199