• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/values.h"
6 #include "chrome/browser/extensions/api/tabs/tabs_api.h"
7 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/browser/extensions/extension_function_test_utils.h"
10 #include "chrome/browser/extensions/extension_tab_util.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "chrome/test/base/interactive_test_utils.h"
15 #include "chrome/test/base/ui_test_utils.h"
16 
17 namespace keys = extensions::tabs_constants;
18 namespace utils = extension_function_test_utils;
19 
20 typedef InProcessBrowserTest ExtensionTabsTest;
21 
22 // http://crbug.com/154081 for Aura specific
23 // http://crbug.com/179063 for other general failures on try bots.
24 #if defined(OS_WIN)
25 #define MAYBE_GetLastFocusedWindow DISABLED_GetLastFocusedWindow
26 #else
27 #define MAYBE_GetLastFocusedWindow GetLastFocusedWindow
28 #endif
29 
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest,MAYBE_GetLastFocusedWindow)30 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, MAYBE_GetLastFocusedWindow) {
31   // Create a new window which making it the "last focused" window.
32   // Note that "last focused" means the "top" most window.
33   Browser* new_browser = CreateBrowser(browser()->profile());
34   int focused_window_id =
35       extensions::ExtensionTabUtil::GetWindowId(new_browser);
36 
37   scoped_refptr<extensions::WindowsGetLastFocusedFunction> function =
38       new extensions::WindowsGetLastFocusedFunction();
39   scoped_refptr<extensions::Extension> extension(utils::CreateEmptyExtension());
40   function->set_extension(extension.get());
41   scoped_ptr<base::DictionaryValue> result(utils::ToDictionary(
42       utils::RunFunctionAndReturnSingleResult(function.get(),
43                                               "[]",
44                                               new_browser)));
45 
46   // The id should always match the last focused window and does not depend
47   // on what was passed to RunFunctionAndReturnSingleResult.
48   EXPECT_EQ(focused_window_id, utils::GetInteger(result.get(), "id"));
49   base::ListValue* tabs = NULL;
50   EXPECT_FALSE(result.get()->GetList(keys::kTabsKey, &tabs));
51 
52   function = new extensions::WindowsGetLastFocusedFunction();
53   function->set_extension(extension.get());
54   result.reset(utils::ToDictionary(
55       utils::RunFunctionAndReturnSingleResult(function.get(),
56                                               "[{\"populate\": true}]",
57                                               browser())));
58 
59   // The id should always match the last focused window and does not depend
60   // on what was passed to RunFunctionAndReturnSingleResult.
61   EXPECT_EQ(focused_window_id, utils::GetInteger(result.get(), "id"));
62   // "populate" was enabled so tabs should be populated.
63   EXPECT_TRUE(result.get()->GetList(keys::kTabsKey, &tabs));
64 }
65 
66 // Flaky: http://crbug.com/136562
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest,DISABLED_QueryLastFocusedWindowTabs)67 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DISABLED_QueryLastFocusedWindowTabs) {
68   const size_t kExtraWindows = 2;
69   for (size_t i = 0; i < kExtraWindows; ++i)
70     CreateBrowser(browser()->profile());
71 
72   Browser* focused_window = CreateBrowser(browser()->profile());
73 #if defined(OS_MACOSX)
74   // See BrowserWindowCocoa::Show. In tests, Browser::window()->IsActive won't
75   // work unless we fake the browser being launched by the user.
76   ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(
77       focused_window->window()->GetNativeWindow()));
78 #endif
79 
80   // Needed on Mac and Linux so that the BrowserWindow::IsActive calls work.
81   content::RunAllPendingInMessageLoop();
82 
83   GURL url;
84   AddTabAtIndexToBrowser(focused_window, 0, url, content::PAGE_TRANSITION_LINK);
85   int focused_window_id =
86       extensions::ExtensionTabUtil::GetWindowId(focused_window);
87 
88   // Get tabs in the 'last focused' window called from non-focused browser.
89   scoped_refptr<extensions::TabsQueryFunction> function =
90       new extensions::TabsQueryFunction();
91   scoped_ptr<base::ListValue> result(utils::ToList(
92       utils::RunFunctionAndReturnSingleResult(function.get(),
93                                               "[{\"lastFocusedWindow\":true}]",
94                                               browser())));
95 
96   base::ListValue* result_tabs = result.get();
97   // We should have one initial tab and one added tab.
98   EXPECT_EQ(2u, result_tabs->GetSize());
99   for (size_t i = 0; i < result_tabs->GetSize(); ++i) {
100     base::DictionaryValue* result_tab = NULL;
101     EXPECT_TRUE(result_tabs->GetDictionary(i, &result_tab));
102     EXPECT_EQ(focused_window_id, utils::GetInteger(result_tab,
103                                                    keys::kWindowIdKey));
104   }
105 
106   // Get tabs NOT in the 'last focused' window called from the focused browser.
107   function = new extensions::TabsQueryFunction();
108   result.reset(utils::ToList(
109       utils::RunFunctionAndReturnSingleResult(function.get(),
110                                               "[{\"lastFocusedWindow\":false}]",
111                                               browser())));
112 
113   result_tabs = result.get();
114   // We should get one tab for each extra window and one for the initial window.
115   EXPECT_EQ(kExtraWindows + 1, result_tabs->GetSize());
116   for (size_t i = 0; i < result_tabs->GetSize(); ++i) {
117     base::DictionaryValue* result_tab = NULL;
118     EXPECT_TRUE(result_tabs->GetDictionary(i, &result_tab));
119     EXPECT_NE(focused_window_id, utils::GetInteger(result_tab,
120                                                    keys::kWindowIdKey));
121   }
122 }
123 
124 #if defined(OS_WIN)  // http://crbug.com/154081 && http://crbug.com/171080
125 #define MAYBE_TabCurrentWindow DISABLED_TabCurrentWindow
126 #else
127 #define MAYBE_TabCurrentWindow TabCurrentWindow
128 #endif
IN_PROC_BROWSER_TEST_F(ExtensionApiTest,MAYBE_TabCurrentWindow)129 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_TabCurrentWindow) {
130   ASSERT_TRUE(RunExtensionTest("tabs/current_window")) << message_;
131 }
132