[M108 Migration][Callback] Bring up ewk callbacks which are related with page loading
[platform/framework/web/chromium-efl.git] / apps / load_and_launch_browsertest.cc
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Tests for the --load-and-launch-app switch.
6 // The two cases are when chrome is running and another process uses the switch
7 // and when chrome is started from scratch.
8
9 #include <iterator>
10
11 #include "apps/switches.h"
12 #include "base/command_line.h"
13 #include "base/process/launch.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/test/test_switches.h"
16 #include "base/test/test_timeouts.h"
17 #include "build/branding_buildflags.h"
18 #include "build/build_config.h"
19 #include "build/chromeos_buildflags.h"
20 #include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
21 #include "chrome/browser/extensions/extension_browsertest.h"
22 #include "chrome/browser/extensions/load_error_reporter.h"
23 #include "chrome/browser/profiles/profile_manager.h"
24 #include "chrome/browser/ui/simple_message_box_internal.h"
25 #include "chrome/common/chrome_result_codes.h"
26 #include "chrome/common/chrome_switches.h"
27 #include "content/public/common/content_switches.h"
28 #include "content/public/test/browser_test.h"
29 #include "content/public/test/test_launcher.h"
30 #include "extensions/browser/extension_registry.h"
31 #include "extensions/test/extension_test_message_listener.h"
32 #include "sandbox/policy/switches.h"
33
34 using extensions::PlatformAppBrowserTest;
35
36 namespace apps {
37
38 namespace {
39
40 constexpr char kTestExtensionId[] = "behllobkkfkfnphdnhnkndlbkcpglgmj";
41
42 // Lacros doesn't support launching with chrome already running. See the header
43 // comment for InProcessBrowserTest::GetCommandLineForRelaunch().
44 #if !BUILDFLAG(IS_CHROMEOS_LACROS)
45
46 const char* kSwitchesToCopy[] = {
47     sandbox::policy::switches::kNoSandbox,
48     switches::kUserDataDir,
49 };
50
51 // TODO(jackhou): Enable this test once it works on OSX. It currently does not
52 // work for the same reason --app-id doesn't. See http://crbug.com/148465
53 #if BUILDFLAG(IS_MAC)
54 #define MAYBE_LoadAndLaunchAppChromeRunning \
55         DISABLED_LoadAndLaunchAppChromeRunning
56 #else
57 #define MAYBE_LoadAndLaunchAppChromeRunning LoadAndLaunchAppChromeRunning
58 #endif
59
60 // Case where Chrome is already running.
61 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
62                        MAYBE_LoadAndLaunchAppChromeRunning) {
63   ExtensionTestMessageListener launched_listener("Launched");
64
65   const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess();
66   base::CommandLine new_cmdline(cmdline.GetProgram());
67   new_cmdline.CopySwitchesFrom(cmdline, kSwitchesToCopy,
68                                std::size(kSwitchesToCopy));
69
70   base::FilePath app_path = test_data_dir_
71       .AppendASCII("platform_apps")
72       .AppendASCII("minimal");
73
74   new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp,
75                                  app_path.value());
76
77   new_cmdline.AppendSwitch(switches::kLaunchAsBrowser);
78   base::Process process =
79       base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest());
80   ASSERT_TRUE(process.IsValid());
81
82   ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
83   int exit_code;
84   ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_timeout(),
85                                              &exit_code));
86   ASSERT_EQ(chrome::RESULT_CODE_NORMAL_EXIT_PROCESS_NOTIFIED, exit_code);
87 }
88
89 // TODO(jackhou): Enable this test once it works on OSX. It currently does not
90 // work for the same reason --app-id doesn't. See http://crbug.com/148465.
91 #if BUILDFLAG(IS_MAC)
92 #define MAYBE_LoadAndLaunchAppWithFile DISABLED_LoadAndLaunchAppWithFile
93 #else
94 #define MAYBE_LoadAndLaunchAppWithFile LoadAndLaunchAppWithFile
95 #endif
96
97 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
98                        MAYBE_LoadAndLaunchAppWithFile) {
99   ExtensionTestMessageListener launched_listener("Launched");
100
101   const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess();
102   base::CommandLine new_cmdline(cmdline.GetProgram());
103   new_cmdline.CopySwitchesFrom(cmdline, kSwitchesToCopy,
104                                std::size(kSwitchesToCopy));
105
106   base::FilePath app_path = test_data_dir_
107       .AppendASCII("platform_apps")
108       .AppendASCII("load_and_launch_file");
109
110   base::FilePath test_file_path = test_data_dir_
111       .AppendASCII("platform_apps")
112       .AppendASCII("launch_files")
113       .AppendASCII("test.txt");
114
115   new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp,
116                                  app_path.value());
117   new_cmdline.AppendSwitch(switches::kLaunchAsBrowser);
118   new_cmdline.AppendArgPath(test_file_path);
119
120   base::Process process =
121       base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest());
122   ASSERT_TRUE(process.IsValid());
123
124   ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
125   int exit_code;
126   ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_timeout(),
127                                              &exit_code));
128   ASSERT_EQ(chrome::RESULT_CODE_NORMAL_EXIT_PROCESS_NOTIFIED, exit_code);
129 }
130
131 #endif  // !BUILDFLAG(IS_CHROMEOS_LACROS)
132
133 // TestFixture that appends --load-and-launch-app with an app before calling
134 // BrowserMain.
135 class LoadAndLaunchPlatformAppBrowserTest : public PlatformAppBrowserTest {
136  public:
137   LoadAndLaunchPlatformAppBrowserTest(
138       const LoadAndLaunchPlatformAppBrowserTest&) = delete;
139   LoadAndLaunchPlatformAppBrowserTest& operator=(
140       const LoadAndLaunchPlatformAppBrowserTest&) = delete;
141
142  protected:
143   LoadAndLaunchPlatformAppBrowserTest() = default;
144
145   void SetUpCommandLine(base::CommandLine* command_line) override {
146     PlatformAppBrowserTest::SetUpCommandLine(command_line);
147     base::FilePath app_path =
148         test_data_dir_.AppendASCII("platform_apps").AppendASCII("minimal");
149     command_line->AppendSwitchNative(apps::kLoadAndLaunchApp, app_path.value());
150
151     // |launched_listener_| needs to be instantiated before the app process is
152     // launched to ensure the test api observer is registered.
153     launched_listener_ =
154         std::make_unique<ExtensionTestMessageListener>("Launched");
155   }
156
157   void TearDownOnMainThread() override { launched_listener_.reset(); }
158
159   void LoadAndLaunchApp() {
160     ASSERT_TRUE(launched_listener_->WaitUntilSatisfied());
161
162     // Start an actual browser because we can't shut down with just an app
163     // window.
164     CreateBrowser(profile());
165   }
166
167   std::unique_ptr<ExtensionTestMessageListener> launched_listener_;
168 };
169
170 // TestFixture that appends --load-and-launch-app with an extension before
171 // calling BrowserMain.
172 class LoadAndLaunchExtensionBrowserTest : public PlatformAppBrowserTest {
173  public:
174   LoadAndLaunchExtensionBrowserTest(const LoadAndLaunchExtensionBrowserTest&) =
175       delete;
176   LoadAndLaunchExtensionBrowserTest& operator=(
177       const LoadAndLaunchExtensionBrowserTest&) = delete;
178
179  protected:
180   LoadAndLaunchExtensionBrowserTest() = default;
181
182   void SetUpCommandLine(base::CommandLine* command_line) override {
183     PlatformAppBrowserTest::SetUpCommandLine(command_line);
184     base::FilePath app_path = test_data_dir_.AppendASCII("good")
185                                   .AppendASCII("Extensions")
186                                   .AppendASCII(kTestExtensionId)
187                                   .AppendASCII("1.0.0.0");
188     command_line->AppendSwitchNative(apps::kLoadAndLaunchApp, app_path.value());
189   }
190
191   void SetUpInProcessBrowserTestFixture() override {
192     PlatformAppBrowserTest::SetUpInProcessBrowserTestFixture();
193
194     // Skip showing the error message box to avoid freezing the main thread.
195     chrome::internal::g_should_skip_message_box_for_test = true;
196   }
197 };
198
199 // Case where Chrome is not running.
200 IN_PROC_BROWSER_TEST_F(LoadAndLaunchPlatformAppBrowserTest,
201                        LoadAndLaunchAppChromeNotRunning) {
202   LoadAndLaunchApp();
203 }
204
205 IN_PROC_BROWSER_TEST_F(LoadAndLaunchExtensionBrowserTest,
206                        LoadAndLaunchExtension) {
207   const std::vector<std::u16string>* errors =
208       extensions::LoadErrorReporter::GetInstance()->GetErrors();
209
210 #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
211   // The error is skipped on official builds.
212   EXPECT_TRUE(errors->empty());
213 #else
214   // Expect |extension_instead_of_app_error|.
215   EXPECT_EQ(1u, errors->size());
216   EXPECT_NE(std::u16string::npos,
217             errors->at(0).find(
218                 u"App loading flags cannot be used to load extensions"));
219 #endif
220
221   extensions::ExtensionRegistry* registry =
222       extensions::ExtensionRegistry::Get(profile());
223   EXPECT_EQ(nullptr,
224             registry->GetExtensionById(
225                 kTestExtensionId, extensions::ExtensionRegistry::EVERYTHING));
226 }
227
228 }  // namespace
229 }  // namespace apps