- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / perf / indexeddb_uitest.cc
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/files/file_path.h"
6 #include "base/path_service.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/test/test_timeouts.h"
9 #include "chrome/common/automation_constants.h"
10 #include "chrome/common/chrome_paths.h"
11 #include "chrome/test/automation/tab_proxy.h"
12 #include "chrome/test/perf/perf_test.h"
13 #include "chrome/test/ui/javascript_test_util.h"
14 #include "chrome/test/ui/ui_perf_test.h"
15 #include "net/base/net_util.h"
16 #include "testing/perf/perf_test.h"
17 #include "url/gurl.h"
18
19 namespace {
20
21 static const base::FilePath::CharType kStartFile[] =
22     FILE_PATH_LITERAL("perf_test.html");
23
24 class IndexedDBTest : public UIPerfTest {
25  public:
26   typedef std::map<std::string, std::string> ResultsMap;
27
28   IndexedDBTest() : reference_(false) {
29     dom_automation_enabled_ = true;
30     show_window_ = true;
31   }
32
33   void RunTest() {
34     base::FilePath::StringType start_file(kStartFile);
35     base::FilePath test_path = GetIndexedDBTestDir();
36     test_path = test_path.Append(start_file);
37     GURL test_url(net::FilePathToFileURL(test_path));
38
39     scoped_refptr<TabProxy> tab(GetActiveTab());
40     ASSERT_TRUE(tab.get());
41     ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURL(test_url));
42
43     // Wait for the test to finish.
44     ASSERT_TRUE(WaitUntilTestCompletes(tab.get(), test_url));
45
46     PrintResults(tab.get());
47   }
48
49  protected:
50   bool reference_;  // True if this is a reference build.
51
52  private:
53   // Return the path to the IndexedDB test directory on the local filesystem.
54   base::FilePath GetIndexedDBTestDir() {
55     base::FilePath test_dir;
56     PathService::Get(chrome::DIR_TEST_DATA, &test_dir);
57     return test_dir.AppendASCII("indexeddb");
58   }
59
60   bool WaitUntilTestCompletes(TabProxy* tab, const GURL& test_url) {
61     return WaitUntilCookieValue(tab, test_url, "__done",
62                                 TestTimeouts::large_test_timeout(), "1");
63   }
64
65   bool GetResults(TabProxy* tab, ResultsMap* results) {
66     std::wstring json_wide;
67     bool succeeded = tab->ExecuteAndExtractString(
68         std::wstring(),
69         L"window.domAutomationController.send("
70         L"    JSON.stringify(automation.getResults()));",
71         &json_wide);
72
73     EXPECT_TRUE(succeeded);
74     if (!succeeded)
75       return false;
76
77     std::string json = WideToUTF8(json_wide);
78     return JsonDictionaryToMap(json, results);
79   }
80
81   void PrintResults(TabProxy* tab) {
82     ResultsMap results;
83     ASSERT_TRUE(GetResults(tab, &results));
84
85     std::string trace_name = reference_ ? "t_ref" : "t";
86
87     ResultsMap::const_iterator it = results.begin();
88     for (; it != results.end(); ++it)
89       perf_test::PrintResultList(
90           it->first, std::string(), trace_name, it->second, "ms", false);
91   }
92
93   DISALLOW_COPY_AND_ASSIGN(IndexedDBTest);
94 };
95
96 class IndexedDBReferenceTest : public IndexedDBTest {
97  public:
98   IndexedDBReferenceTest() : IndexedDBTest() {
99     reference_ = true;
100   }
101
102   virtual void SetUp() {
103     UseReferenceBuild();
104     IndexedDBTest::SetUp();
105   }
106 };
107
108 TEST_F(IndexedDBTest, Perf) {
109
110   RunTest();
111 }
112
113 TEST_F(IndexedDBReferenceTest, Perf) {
114
115   RunTest();
116 }
117
118 }  // namespace