1// Copyright 2014 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/strings/stringprintf.h"
7#include "chrome/browser/extensions/extension_apitest.h"
8#include "chrome/test/base/ui_test_utils.h"
9#include "extensions/common/feature_switch.h"
10#include "extensions/common/switches.h"
11#include "extensions/test/result_catcher.h"
12
13using extensions::Extension;
14using extensions::FeatureSwitch;
15
16class ExtensionOptionsApiTest : public ExtensionApiTest {
17 private:
18  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
19    ExtensionApiTest::SetUpCommandLine(command_line);
20
21    enable_options_.reset(new FeatureSwitch::ScopedOverride(
22        FeatureSwitch::embedded_extension_options(), true));
23    // Need to add a command line flag as well as a FeatureSwitch because the
24    // FeatureSwitch is not copied over to the renderer process from the
25    // browser process.
26    command_line->AppendSwitch(
27        extensions::switches::kEnableEmbeddedExtensionOptions);
28  }
29
30  scoped_ptr<FeatureSwitch::ScopedOverride> enable_options_;
31};
32
33// crbug/415949.
34#if defined(OS_MACOSX)
35#define MAYBE_ExtensionCanEmbedOwnOptions DISABLED_ExtensionCanEmbedOwnOptions
36#else
37#define MAYBE_ExtensionCanEmbedOwnOptions ExtensionCanEmbedOwnOptions
38#endif
39IN_PROC_BROWSER_TEST_F(ExtensionOptionsApiTest,
40                       MAYBE_ExtensionCanEmbedOwnOptions) {
41  base::FilePath extension_dir =
42      test_data_dir_.AppendASCII("extension_options").AppendASCII("embed_self");
43  ASSERT_TRUE(LoadExtension(extension_dir));
44  ASSERT_TRUE(RunExtensionSubtest("extension_options/embed_self", "test.html"));
45}
46
47IN_PROC_BROWSER_TEST_F(ExtensionOptionsApiTest,
48                       ShouldNotEmbedOtherExtensionsOptions) {
49  base::FilePath dir = test_data_dir_.AppendASCII("extension_options")
50                           .AppendASCII("embed_other");
51
52  const Extension* embedder = InstallExtension(dir.AppendASCII("embedder"), 1);
53  const Extension* embedded = InstallExtension(dir.AppendASCII("embedded"), 1);
54
55  ASSERT_TRUE(embedder);
56  ASSERT_TRUE(embedded);
57
58  // Since the extension id of the embedded extension is not always the same,
59  // store the embedded extension id in the embedder's storage before running
60  // the tests.
61  std::string script = base::StringPrintf(
62      "chrome.storage.local.set({'embeddedId': '%s'}, function() {"
63      "window.domAutomationController.send('done injecting');});",
64      embedded->id().c_str());
65
66  ExecuteScriptInBackgroundPage(embedder->id(), script);
67  extensions::ResultCatcher catcher;
68  ui_test_utils::NavigateToURL(browser(),
69                               embedder->GetResourceURL("test.html"));
70  ASSERT_TRUE(catcher.GetNextResult());
71}
72
73IN_PROC_BROWSER_TEST_F(ExtensionOptionsApiTest,
74                       CannotEmbedUsingInvalidExtensionIds) {
75  ASSERT_TRUE(InstallExtension(test_data_dir_.AppendASCII("extension_options")
76                                   .AppendASCII("embed_invalid"),
77                               1));
78  ASSERT_TRUE(
79      RunExtensionSubtest("extension_options/embed_invalid", "test.html"));
80}
81