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