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.
4
5#include "base/logging.h"
6#include "chrome/browser/extensions/extension_apitest.h"
7#include "chrome/browser/extensions/extension_service.h"
8#include "chrome/browser/profiles/profile.h"
9#include "chrome/browser/ui/browser.h"
10#include "chrome/browser/ui/tabs/tab_strip_model.h"
11#include "chrome/test/base/ui_test_utils.h"
12#include "content/public/browser/web_contents.h"
13#include "content/public/test/browser_test_utils.h"
14#include "extensions/common/switches.h"
15#include "net/dns/mock_host_resolver.h"
16#include "url/gurl.h"
17
18class ExtensionIconSourceTest : public ExtensionApiTest {
19 protected:
20  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
21    ExtensionApiTest::SetUpCommandLine(command_line);
22    command_line->AppendSwitch(
23        extensions::switches::kAllowLegacyExtensionManifests);
24  }
25};
26
27// Times out on Mac and Win. http://crbug.com/238705
28#if defined(OS_WIN) || defined(OS_MACOSX)
29#define MAYBE_IconsLoaded DISABLED_IconsLoaded
30#else
31#define MAYBE_IconsLoaded IconsLoaded
32#endif
33
34IN_PROC_BROWSER_TEST_F(ExtensionIconSourceTest, MAYBE_IconsLoaded) {
35  base::FilePath basedir = test_data_dir_.AppendASCII("icons");
36  ASSERT_TRUE(LoadExtension(basedir.AppendASCII("extension_with_permission")));
37  ASSERT_TRUE(LoadExtension(basedir.AppendASCII("extension_no_permission")));
38  std::string result;
39
40  // Test that the icons are loaded and that the chrome://extension-icon
41  // parameters work correctly.
42  ui_test_utils::NavigateToURL(
43      browser(),
44      GURL("chrome-extension://gbmgkahjioeacddebbnengilkgbkhodg/index.html"));
45  ASSERT_TRUE(content::ExecuteScriptAndExtractString(
46      browser()->tab_strip_model()->GetActiveWebContents(),
47      "window.domAutomationController.send(document.title)",
48      &result));
49  EXPECT_EQ(result, "Loaded");
50
51  // Verify that the an extension can't load chrome://extension-icon icons
52  // without the management permission.
53  ui_test_utils::NavigateToURL(
54      browser(),
55      GURL("chrome-extension://apocjbpjpkghdepdngjlknfpmabcmlao/index.html"));
56  ASSERT_TRUE(content::ExecuteScriptAndExtractString(
57      browser()->tab_strip_model()->GetActiveWebContents(),
58      "window.domAutomationController.send(document.title)",
59      &result));
60  EXPECT_EQ(result, "Not Loaded");
61}
62
63IN_PROC_BROWSER_TEST_F(ExtensionIconSourceTest, InvalidURL) {
64  std::string result;
65
66  // Test that navigation to an invalid url works.
67  ui_test_utils::NavigateToURL(
68      browser(),
69      GURL("chrome://extension-icon/invalid"));
70
71  ASSERT_TRUE(content::ExecuteScriptAndExtractString(
72      browser()->tab_strip_model()->GetActiveWebContents(),
73      "window.domAutomationController.send(document.title)",
74      &result));
75  EXPECT_EQ(result, "invalid (96\xC3\x97""96)");
76}
77
78// Times out on Mac and Win. http://crbug.com/238705
79#if defined(OS_WIN) || defined(OS_MACOSX)
80#define MAYBE_IconsLoadedIncognito DISABLED_IconsLoadedIncognito
81#else
82#define MAYBE_IconsLoadedIncognito IconsLoadedIncognito
83#endif
84
85IN_PROC_BROWSER_TEST_F(ExtensionIconSourceTest, MAYBE_IconsLoadedIncognito) {
86  base::FilePath basedir = test_data_dir_.AppendASCII("icons");
87  ASSERT_TRUE(LoadExtensionIncognito(
88      basedir.AppendASCII("extension_with_permission")));
89  ASSERT_TRUE(LoadExtensionIncognito(
90      basedir.AppendASCII("extension_no_permission")));
91  std::string result;
92
93  // Test that the icons are loaded and that the chrome://extension-icon
94  // parameters work correctly.
95  Browser* otr_browser = ui_test_utils::OpenURLOffTheRecord(
96      browser()->profile(),
97      GURL("chrome-extension://gbmgkahjioeacddebbnengilkgbkhodg/index.html"));
98  ASSERT_TRUE(content::ExecuteScriptAndExtractString(
99      otr_browser->tab_strip_model()->GetActiveWebContents(),
100      "window.domAutomationController.send(document.title)",
101      &result));
102  EXPECT_EQ(result, "Loaded");
103
104  // Verify that the an extension can't load chrome://extension-icon icons
105  // without the management permission.
106  ui_test_utils::OpenURLOffTheRecord(
107      browser()->profile(),
108      GURL("chrome-extension://apocjbpjpkghdepdngjlknfpmabcmlao/index.html"));
109  ASSERT_TRUE(content::ExecuteScriptAndExtractString(
110      otr_browser->tab_strip_model()->GetActiveWebContents(),
111      "window.domAutomationController.send(document.title)",
112      &result));
113  EXPECT_EQ(result, "Not Loaded");
114}
115