45c9740de25ada25b1cb9e3fafbdbe30d6537189
[platform/framework/web/crosswalk.git] / src / xwalk / test / base / xwalk_test_utils.cc
1 // Copyright (c) 2013 Intel Corporation. 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 "xwalk/test/base/xwalk_test_utils.h"
6
7 #include "base/command_line.h"
8 #include "base/environment.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/path_service.h"
12 #include "base/run_loop.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "xwalk/runtime/browser/runtime.h"
15 #include "xwalk/runtime/common/xwalk_paths.h"
16 #include "xwalk/runtime/common/xwalk_switches.h"
17 #include "content/public/browser/navigation_controller.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/notification_source.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/common/content_switches.h"
22 #include "content/public/test/browser_test_utils.h"
23 #include "content/public/test/test_navigation_observer.h"
24 #include "content/public/test/test_utils.h"
25 #include "net/base/net_util.h"
26 #include "ui/gl/gl_switches.h"
27
28 using content::TestNavigationObserver;
29 using content::WebContents;
30
31 namespace xwalk_test_utils {
32
33 void PrepareBrowserCommandLineForTests(CommandLine* command_line) {
34   // Enable info level logging by default so that we can see when bad
35   // stuff happens, but honor the flags specified from the command line.
36   if (!command_line->HasSwitch(switches::kEnableLogging))
37     command_line->AppendSwitch(switches::kEnableLogging);
38   if (!command_line->HasSwitch(switches::kLoggingLevel))
39     command_line->AppendSwitchASCII(switches::kLoggingLevel, "0");
40
41   // Don't collect GPU info, load GPU blacklist, or schedule a GPU blacklist
42   // auto-update.
43   command_line->AppendSwitch(switches::kSkipGpuDataLoading);
44 }
45
46 bool OverrideDataPathDir(const base::FilePath& data_path_dir) {
47   // PathService::Override() is the best way to change the data path directory.
48   return PathService::Override(xwalk::DIR_DATA_PATH, data_path_dir);
49 }
50
51 base::FilePath GetTestFilePath(const base::FilePath& dir,
52                                const base::FilePath& file) {
53   base::FilePath test_base_dir;
54   PathService::Get(xwalk::DIR_TEST_DATA, &test_base_dir);
55
56   return test_base_dir.Append(dir).Append(file);
57 }
58
59 GURL GetTestURL(const base::FilePath& dir, const base::FilePath& file) {
60   return net::FilePathToFileURL(GetTestFilePath(dir, file));
61 }
62
63 // Navigate a specified URL in the given Runtime. It will block until the
64 // navigation completes.
65 void NavigateToURL(xwalk::Runtime* runtime, const GURL& url) {
66   if (runtime->web_contents()->IsLoading())
67     content::WaitForLoadStop(runtime->web_contents());
68
69   TestNavigationObserver navigation_observer(runtime->web_contents(), 1);
70   runtime->LoadURL(url);
71
72   base::RunLoop run_loop;
73   navigation_observer.Wait();
74 }
75
76 }  // namespace xwalk_test_utils