summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_tabs_test.cc
blob: 3428abce0d1f2624fa45c286173fe6b1e8e7bb16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/extension_tabs_module.h"

#include <string>

#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/stringprintf.h"
#include "base/string_util.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_function_test_utils.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/extensions/extension_tabs_module_constants.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "ui/gfx/rect.h"

using namespace extension_function_test_utils;

namespace {

class ExtensionTabsTest : public InProcessBrowserTest {
};

}

IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetWindow) {
  int window_id = ExtensionTabUtil::GetWindowId(browser());

  // Invalid window ID error.
  EXPECT_TRUE(MatchPattern(
      RunFunctionAndReturnError(
          new GetWindowFunction(),
          base::StringPrintf("[%u]", window_id + 1),
          browser()),
      extension_tabs_module_constants::kWindowNotFoundError));

  // Basic window details.
  // Need GetRestoredBound instead of GetBounds.
  // See crbug.com/98759.
  gfx::Rect bounds = browser()->window()->GetRestoredBounds();

  scoped_ptr<base::DictionaryValue> result(ToDictionary(
      RunFunctionAndReturnResult(
          new GetWindowFunction(),
          base::StringPrintf("[%u]", window_id),
          browser())));
  EXPECT_EQ(window_id, GetInteger(result.get(), "id"));
  EXPECT_FALSE(GetBoolean(result.get(), "incognito"));
  EXPECT_EQ("normal", GetString(result.get(), "type"));
  EXPECT_EQ(bounds.x(), GetInteger(result.get(), "left"));
  EXPECT_EQ(bounds.y(), GetInteger(result.get(), "top"));
  EXPECT_EQ(bounds.width(), GetInteger(result.get(), "width"));
  EXPECT_EQ(bounds.height(), GetInteger(result.get(), "height"));

  // TODO(aa): Can't assume window is focused. On mac, calling Activate() from a
  // browser test doesn't seem to do anything, so can't test the opposite
  // either.
  EXPECT_EQ(browser()->window()->IsActive(),
            GetBoolean(result.get(), "focused"));

  // TODO(aa): Minimized and maximized dimensions. Is there a way to set
  // minimize/maximize programmatically?

  // Popup.
  Browser* popup_browser =
      Browser::CreateForType(Browser::TYPE_POPUP, browser()->profile());
  result.reset(ToDictionary(
      RunFunctionAndReturnResult(
          new GetWindowFunction(),
          base::StringPrintf(
              "[%u]", ExtensionTabUtil::GetWindowId(popup_browser)),
          browser())));
  EXPECT_EQ("popup", GetString(result.get(), "type"));

  // Panel.
  Browser* panel_browser =
      Browser::CreateForType(Browser::TYPE_PANEL, browser()->profile());
  result.reset(ToDictionary(
      RunFunctionAndReturnResult(
          new GetWindowFunction(),
          base::StringPrintf(
              "[%u]", ExtensionTabUtil::GetWindowId(panel_browser)),
          browser())));
  EXPECT_EQ("panel", GetString(result.get(), "type"));

  // Incognito.
  Browser* incognito_browser = CreateIncognitoBrowser();
  int incognito_window_id = ExtensionTabUtil::GetWindowId(incognito_browser);

  // Without "include_incognito".
  EXPECT_TRUE(MatchPattern(
      RunFunctionAndReturnError(
          new GetWindowFunction(),
          base::StringPrintf("[%u]", incognito_window_id),
          browser()),
      extension_tabs_module_constants::kWindowNotFoundError));

  // With "include_incognito".
  result.reset(ToDictionary(
      RunFunctionAndReturnResult(
          new GetWindowFunction(),
          base::StringPrintf("[%u]", incognito_window_id),
          browser(),
          INCLUDE_INCOGNITO)));
  EXPECT_TRUE(GetBoolean(result.get(), "incognito"));
}

IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, UpdateNoPermissions) {
  // The test empty extension has no permissions, therefore it should not get
  // tab data in the function result.
  scoped_refptr<UpdateTabFunction> update_tab_function(new UpdateTabFunction());
  scoped_refptr<Extension> empty_extension(CreateEmptyExtension());
  update_tab_function->set_extension(empty_extension.get());
  // Without a callback the function will not generate a result.
  update_tab_function->set_has_callback(true);

  scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
          update_tab_function.get(),
          "[null, {\"url\": \"neutrinos\"}]",
          browser()));
  EXPECT_EQ(base::Value::TYPE_NULL, result->GetType());
}