extension_incognito_apitest.cc revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2010 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 "chrome/browser/browser.h"
6#include "chrome/browser/browser_list.h"
7#include "chrome/browser/browser_window.h"
8#include "chrome/browser/extensions/browser_action_test_util.h"
9#include "chrome/browser/extensions/extension_apitest.h"
10#include "chrome/browser/extensions/extensions_service.h"
11#include "chrome/browser/extensions/user_script_master.h"
12#include "chrome/browser/profile.h"
13#include "chrome/browser/tab_contents/tab_contents.h"
14#include "chrome/common/chrome_switches.h"
15#include "chrome/common/url_constants.h"
16#include "chrome/test/ui_test_utils.h"
17#include "net/base/mock_host_resolver.h"
18
19IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, IncognitoNoScript) {
20  host_resolver()->AddRule("*", "127.0.0.1");
21  ASSERT_TRUE(StartHTTPServer());
22
23  // Loads a simple extension which attempts to change the title of every page
24  // that loads to "modified".
25  ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("api_test")
26      .AppendASCII("incognito").AppendASCII("content_scripts")));
27
28  // Open incognito window and navigate to test page.
29  ui_test_utils::OpenURLOffTheRecord(browser()->profile(),
30      GURL("http://www.example.com:1337/files/extensions/test_file.html"));
31  Browser* otr_browser = BrowserList::FindBrowserWithType(
32      browser()->profile()->GetOffTheRecordProfile(), Browser::TYPE_NORMAL,
33      false);
34  TabContents* tab = otr_browser->GetSelectedTabContents();
35
36  // Verify the script didn't run.
37  bool result = false;
38  ui_test_utils::ExecuteJavaScriptAndExtractBool(
39      tab->render_view_host(), L"",
40      L"window.domAutomationController.send(document.title == 'Unmodified')",
41      &result);
42  EXPECT_TRUE(result);
43}
44
45IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, IncognitoYesScript) {
46  host_resolver()->AddRule("*", "127.0.0.1");
47  ASSERT_TRUE(StartHTTPServer());
48
49  // Load a dummy extension. This just tests that we don't regress a
50  // crash fix when multiple incognito- and non-incognito-enabled extensions
51  // are mixed.
52  ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("api_test")
53      .AppendASCII("content_scripts").AppendASCII("all_frames")));
54
55  // Loads a simple extension which attempts to change the title of every page
56  // that loads to "modified".
57  ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_.AppendASCII("api_test")
58      .AppendASCII("incognito").AppendASCII("content_scripts")));
59
60  // Dummy extension #2.
61  ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("api_test")
62      .AppendASCII("content_scripts").AppendASCII("isolated_world1")));
63
64  // Open incognito window and navigate to test page.
65  ui_test_utils::OpenURLOffTheRecord(browser()->profile(),
66      GURL("http://www.example.com:1337/files/extensions/test_file.html"));
67  Browser* otr_browser = BrowserList::FindBrowserWithType(
68      browser()->profile()->GetOffTheRecordProfile(), Browser::TYPE_NORMAL,
69      false);
70  TabContents* tab = otr_browser->GetSelectedTabContents();
71
72  // Verify the script ran.
73  bool result = false;
74  ui_test_utils::ExecuteJavaScriptAndExtractBool(
75      tab->render_view_host(), L"",
76      L"window.domAutomationController.send(document.title == 'modified')",
77      &result);
78  EXPECT_TRUE(result);
79}
80
81// Tests that the APIs in an incognito-enabled extension work properly.
82// Flaky, http://crbug.com/42844.
83IN_PROC_BROWSER_TEST_F(ExtensionApiTest, FLAKY_Incognito) {
84  host_resolver()->AddRule("*", "127.0.0.1");
85  ASSERT_TRUE(StartHTTPServer());
86
87  ResultCatcher catcher;
88
89  // Open incognito window and navigate to test page.
90  ui_test_utils::OpenURLOffTheRecord(browser()->profile(),
91      GURL("http://www.example.com:1337/files/extensions/test_file.html"));
92
93  ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
94      .AppendASCII("incognito").AppendASCII("apis")));
95
96  EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
97}
98
99// Tests that the APIs in an incognito-disabled extension don't see incognito
100// events or callbacks.
101IN_PROC_BROWSER_TEST_F(ExtensionApiTest, IncognitoDisabled) {
102  host_resolver()->AddRule("*", "127.0.0.1");
103  ASSERT_TRUE(StartHTTPServer());
104
105  ResultCatcher catcher;
106
107  // Open incognito window and navigate to test page.
108  ui_test_utils::OpenURLOffTheRecord(browser()->profile(),
109      GURL("http://www.example.com:1337/files/extensions/test_file.html"));
110
111  ASSERT_TRUE(LoadExtension(test_data_dir_
112      .AppendASCII("incognito").AppendASCII("apis_disabled")));
113
114  EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
115}
116
117// Test that opening a popup from an incognito browser window works properly.
118IN_PROC_BROWSER_TEST_F(ExtensionApiTest, IncognitoPopup) {
119  host_resolver()->AddRule("*", "127.0.0.1");
120  ASSERT_TRUE(StartHTTPServer());
121
122  ResultCatcher catcher;
123
124  ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
125      .AppendASCII("incognito").AppendASCII("popup")));
126
127  // Open incognito window and navigate to test page.
128  ui_test_utils::OpenURLOffTheRecord(browser()->profile(),
129      GURL("http://www.example.com:1337/files/extensions/test_file.html"));
130  Browser* incognito_browser = BrowserList::FindBrowserWithType(
131      browser()->profile()->GetOffTheRecordProfile(), Browser::TYPE_NORMAL,
132      false);
133
134  // Simulate the incognito's browser action being clicked.
135  BrowserActionTestUtil(incognito_browser).Press(0);
136
137  EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
138}
139