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.
5 #include "base/path_service.h"
6 #include "base/strings/stringprintf.h"
7 #include "chrome/browser/devtools/devtools_window.h"
8 #include "chrome/browser/extensions/api/developer_private/developer_private_api.h"
9 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/browser/extensions/extension_function_test_utils.h"
11 #include "chrome/common/chrome_paths.h"
12 #include "extensions/browser/app_window/app_window.h"
13 #include "extensions/browser/app_window/app_window_registry.h"
15 namespace extensions
{
17 using DeveloperPrivateApiTest
= ExtensionApiTest
;
19 IN_PROC_BROWSER_TEST_F(DeveloperPrivateApiTest
, Basics
) {
20 // Load up some extensions so that we can query their info and adjust their
21 // setings in the API test.
22 base::FilePath base_dir
= test_data_dir_
.AppendASCII("developer");
23 EXPECT_TRUE(LoadExtension(base_dir
.AppendASCII("hosted_app")));
24 EXPECT_TRUE(InstallExtension(
25 base_dir
.AppendASCII("packaged_app"), 1, Manifest::INTERNAL
));
26 LoadExtension(base_dir
.AppendASCII("simple_extension"));
28 ASSERT_TRUE(RunPlatformAppTestWithFlags(
29 "developer/test", kFlagLoadAsComponent
));
32 // Tests opening the developer tools for an app window.
33 IN_PROC_BROWSER_TEST_F(DeveloperPrivateApiTest
, InspectAppWindowView
) {
35 PathService::Get(chrome::DIR_TEST_DATA
, &dir
);
36 dir
= dir
.AppendASCII("extensions")
37 .AppendASCII("platform_apps")
38 .AppendASCII("minimal");
40 // Load and launch a platform app.
41 const Extension
* app
= LoadAndLaunchApp(dir
);
43 // Get the info about the app, including the inspectable views.
44 scoped_refptr
<UIThreadExtensionFunction
> function(
45 new api::DeveloperPrivateGetExtensionInfoFunction());
46 scoped_ptr
<base::Value
> result(
47 extension_function_test_utils::RunFunctionAndReturnSingleResult(
48 function
.get(), base::StringPrintf("[\"%s\"]", app
->id().c_str()),
51 scoped_ptr
<api::developer_private::ExtensionInfo
> info
=
52 api::developer_private::ExtensionInfo::FromValue(*result
);
55 // There should be two inspectable views - the background page and the app
56 // window. Find the app window.
57 ASSERT_EQ(2u, info
->views
.size());
58 api::developer_private::ExtensionView
* window_view
= nullptr;
59 for (const auto& view
: info
->views
) {
60 if (view
->type
== api::developer_private::VIEW_TYPE_APP_WINDOW
) {
61 window_view
= view
.get();
65 ASSERT_TRUE(window_view
);
67 // Inspect the app window.
68 function
= new api::DeveloperPrivateOpenDevToolsFunction();
69 extension_function_test_utils::RunFunction(
71 base::StringPrintf("[{\"renderViewId\": %d, \"renderProcessId\": %d}]",
72 window_view
->render_view_id
,
73 window_view
->render_process_id
),
74 browser(), extension_function_test_utils::NONE
);
76 // Verify that dev tools opened.
77 std::list
<AppWindow
*> app_windows
=
78 AppWindowRegistry::Get(profile())->GetAppWindowsForApp(app
->id());
79 ASSERT_EQ(1u, app_windows
.size());
80 EXPECT_TRUE(DevToolsWindow::GetInstanceForInspectedWebContents(
81 (*app_windows
.begin())->web_contents()));
84 } // namespace extensions