Upload upstream chromium 76.0.3809.146
[platform/framework/web/chromium-efl.git] / apps / load_and_launch_browsertest.cc
1 // Copyright 2013 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 // 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 "apps/switches.h"
10 #include "base/process/launch.h"
11 #include "base/stl_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/test/test_timeouts.h"
14 #include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
15 #include "chrome/browser/extensions/extension_browsertest.h"
16 #include "chrome/browser/extensions/load_error_reporter.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/browser/ui/simple_message_box_internal.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "content/public/common/content_switches.h"
21 #include "content/public/test/test_launcher.h"
22 #include "extensions/browser/extension_registry.h"
23 #include "extensions/test/extension_test_message_listener.h"
24 #include "services/service_manager/sandbox/switches.h"
25
26 using extensions::PlatformAppBrowserTest;
27
28 namespace apps {
29
30 namespace {
31
32 const char* kSwitchesToCopy[] = {
33     service_manager::switches::kNoSandbox, switches::kUserDataDir,
34 };
35
36 constexpr char kTestExtensionId[] = "behllobkkfkfnphdnhnkndlbkcpglgmj";
37
38 }  // namespace
39
40 // TODO(jackhou): Enable this test once it works on OSX. It currently does not
41 // work for the same reason --app-id doesn't. See http://crbug.com/148465
42 #if defined(OS_MACOSX)
43 #define MAYBE_LoadAndLaunchAppChromeRunning \
44         DISABLED_LoadAndLaunchAppChromeRunning
45 #else
46 #define MAYBE_LoadAndLaunchAppChromeRunning LoadAndLaunchAppChromeRunning
47 #endif
48
49 // Case where Chrome is already running.
50 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
51                        MAYBE_LoadAndLaunchAppChromeRunning) {
52   ExtensionTestMessageListener launched_listener("Launched", false);
53
54   const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess();
55   base::CommandLine new_cmdline(cmdline.GetProgram());
56   new_cmdline.CopySwitchesFrom(cmdline, kSwitchesToCopy,
57                                base::size(kSwitchesToCopy));
58
59   base::FilePath app_path = test_data_dir_
60       .AppendASCII("platform_apps")
61       .AppendASCII("minimal");
62
63   new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp,
64                                  app_path.value());
65
66   new_cmdline.AppendSwitch(content::kLaunchAsBrowser);
67   base::Process process =
68       base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest());
69   ASSERT_TRUE(process.IsValid());
70
71   ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
72   int exit_code;
73   ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_timeout(),
74                                              &exit_code));
75   ASSERT_EQ(0, exit_code);
76 }
77
78 // TODO(jackhou): Enable this test once it works on OSX. It currently does not
79 // work for the same reason --app-id doesn't. See http://crbug.com/148465
80 #if defined(OS_MACOSX)
81 #define MAYBE_LoadAndLaunchAppWithFile DISABLED_LoadAndLaunchAppWithFile
82 #else
83 #define MAYBE_LoadAndLaunchAppWithFile LoadAndLaunchAppWithFile
84 #endif
85
86 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
87                        MAYBE_LoadAndLaunchAppWithFile) {
88   ExtensionTestMessageListener launched_listener("Launched", false);
89
90   const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess();
91   base::CommandLine new_cmdline(cmdline.GetProgram());
92   new_cmdline.CopySwitchesFrom(cmdline, kSwitchesToCopy,
93                                base::size(kSwitchesToCopy));
94
95   base::FilePath app_path = test_data_dir_
96       .AppendASCII("platform_apps")
97       .AppendASCII("load_and_launch_file");
98
99   base::FilePath test_file_path = test_data_dir_
100       .AppendASCII("platform_apps")
101       .AppendASCII("launch_files")
102       .AppendASCII("test.txt");
103
104   new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp,
105                                  app_path.value());
106   new_cmdline.AppendSwitch(content::kLaunchAsBrowser);
107   new_cmdline.AppendArgPath(test_file_path);
108
109   base::Process process =
110       base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest());
111   ASSERT_TRUE(process.IsValid());
112
113   ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
114   int exit_code;
115   ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_timeout(),
116                                              &exit_code));
117   ASSERT_EQ(0, exit_code);
118 }
119
120 namespace {
121
122 // TestFixture that appends --load-and-launch-app with an app before calling
123 // BrowserMain.
124 class LoadAndLaunchPlatformAppBrowserTest : public PlatformAppBrowserTest {
125  protected:
126   LoadAndLaunchPlatformAppBrowserTest() {}
127
128   void SetUpCommandLine(base::CommandLine* command_line) override {
129     PlatformAppBrowserTest::SetUpCommandLine(command_line);
130     base::FilePath app_path =
131         test_data_dir_.AppendASCII("platform_apps").AppendASCII("minimal");
132     command_line->AppendSwitchNative(apps::kLoadAndLaunchApp, app_path.value());
133   }
134
135   void LoadAndLaunchApp() {
136     ExtensionTestMessageListener launched_listener("Launched", false);
137     ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
138
139     // Start an actual browser because we can't shut down with just an app
140     // window.
141     CreateBrowser(ProfileManager::GetActiveUserProfile());
142   }
143
144  private:
145   DISALLOW_COPY_AND_ASSIGN(LoadAndLaunchPlatformAppBrowserTest);
146 };
147
148 // TestFixture that appends --load-and-launch-app with an extension before
149 // calling BrowserMain.
150 class LoadAndLaunchExtensionBrowserTest : public PlatformAppBrowserTest {
151  protected:
152   LoadAndLaunchExtensionBrowserTest() {}
153
154   void SetUpCommandLine(base::CommandLine* command_line) override {
155     PlatformAppBrowserTest::SetUpCommandLine(command_line);
156     base::FilePath app_path = test_data_dir_.AppendASCII("good")
157                                   .AppendASCII("Extensions")
158                                   .AppendASCII(kTestExtensionId)
159                                   .AppendASCII("1.0.0.0");
160     command_line->AppendSwitchNative(apps::kLoadAndLaunchApp, app_path.value());
161   }
162
163   void SetUpInProcessBrowserTestFixture() override {
164     PlatformAppBrowserTest::SetUpInProcessBrowserTestFixture();
165
166     // Skip showing the error message box to avoid freezing the main thread.
167     chrome::internal::g_should_skip_message_box_for_test = true;
168   }
169
170   DISALLOW_COPY_AND_ASSIGN(LoadAndLaunchExtensionBrowserTest);
171 };
172
173 }  // namespace
174
175 // Case where Chrome is not running.
176 IN_PROC_BROWSER_TEST_F(LoadAndLaunchPlatformAppBrowserTest,
177                        LoadAndLaunchAppChromeNotRunning) {
178   LoadAndLaunchApp();
179 }
180
181 IN_PROC_BROWSER_TEST_F(LoadAndLaunchExtensionBrowserTest,
182                        LoadAndLaunchExtension) {
183   const std::vector<base::string16>* errors =
184       extensions::LoadErrorReporter::GetInstance()->GetErrors();
185
186 #if defined(GOOGLE_CHROME_BUILD)
187   // The error is skipped on official builds.
188   EXPECT_TRUE(errors->empty());
189 #else
190   // Expect |extension_instead_of_app_error|.
191   EXPECT_EQ(1u, errors->size());
192   EXPECT_NE(base::string16::npos,
193             errors->at(0).find(base::ASCIIToUTF16(
194                 "App loading flags cannot be used to load extensions")));
195 #endif
196
197   extensions::ExtensionRegistry* registry =
198       extensions::ExtensionRegistry::Get(profile());
199   EXPECT_EQ(nullptr,
200             registry->GetExtensionById(
201                 kTestExtensionId, extensions::ExtensionRegistry::EVERYTHING));
202 }
203
204 }  // namespace apps