File: component_toolbar_actions_browsertest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (149 lines) | stat: -rw-r--r-- 5,336 bytes parent folder | download
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2014 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 "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h"
#include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/toolbar/browser_actions_container.h"
#include "chrome/browser/ui/views/toolbar/toolbar_action_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "extensions/common/feature_switch.h"
#include "grit/theme_resources.h"
#include "ui/base/resource/resource_bundle.h"

namespace {

const char kMockId[] = "mock_action";

class MockComponentAction : public ToolbarActionViewController {
 public:
  MockComponentAction() : click_count_(0u), id_(kMockId) {}
  ~MockComponentAction() override {}

  // ToolbarActionButtonController:
  const std::string& GetId() const override { return id_; }
  void SetDelegate(ToolbarActionViewDelegate* delegate) override {}
  gfx::Image GetIcon(content::WebContents* web_contents) override {
    return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
        IDR_BROWSER_ACTION);
  }
  gfx::ImageSkia GetIconWithBadge() override {
    return *GetIcon(nullptr).ToImageSkia();
  }
  base::string16 GetActionName() const override {
    return base::ASCIIToUTF16("Component Action");
  }
  base::string16 GetAccessibleName(content::WebContents* web_contents)
      const override {
    return GetActionName();
  }
  base::string16 GetTooltip(content::WebContents* web_contents)
      const override {
    return GetActionName();
  }
  bool IsEnabled(content::WebContents* web_contents) const override {
    return true;
  }
  bool WantsToRun(content::WebContents* web_contents) const override {
    return false;
  }
  bool HasPopup(content::WebContents* web_contents) const override {
    return true;
  }
  void HidePopup() override {}
  gfx::NativeView GetPopupNativeView() override { return nullptr; }
  bool CanDrag() const override { return false; }
  bool IsMenuRunning() const override { return false; }
  bool ExecuteAction(bool by_user) override {
    ++click_count_;
    return false;
  }
  void UpdateState() override {}

  size_t click_count() const { return click_count_; }

 private:
  size_t click_count_;
  std::string id_;

  DISALLOW_COPY_AND_ASSIGN(MockComponentAction);
};

class MockComponentToolbarActionsFactory
    : public ComponentToolbarActionsFactory {
 public:
  MockComponentToolbarActionsFactory();
  virtual ~MockComponentToolbarActionsFactory();

  // ComponentToolbarActionsFactory:
  ScopedVector<ToolbarActionViewController> GetComponentToolbarActions()
      override;

 private:
  DISALLOW_COPY_AND_ASSIGN(MockComponentToolbarActionsFactory);
};

MockComponentToolbarActionsFactory::MockComponentToolbarActionsFactory() {
  ComponentToolbarActionsFactory::SetTestingFactory(this);
}

MockComponentToolbarActionsFactory::~MockComponentToolbarActionsFactory() {
  ComponentToolbarActionsFactory::SetTestingFactory(nullptr);
}

ScopedVector<ToolbarActionViewController>
MockComponentToolbarActionsFactory::GetComponentToolbarActions() {
  ScopedVector<ToolbarActionViewController> component_actions;
  component_actions.push_back(new MockComponentAction());
  return component_actions.Pass();
}

}  // namespace

class ComponentToolbarActionsBrowserTest : public InProcessBrowserTest {
 protected:
  ComponentToolbarActionsBrowserTest() {}
  ~ComponentToolbarActionsBrowserTest() override {}

  void SetUpCommandLine(base::CommandLine* command_line) override {
    InProcessBrowserTest::SetUpCommandLine(command_line);
    enable_redesign_.reset(new extensions::FeatureSwitch::ScopedOverride(
        extensions::FeatureSwitch::extension_action_redesign(), true));
    mock_actions_factory_.reset(new MockComponentToolbarActionsFactory());
  }

 private:
  scoped_ptr<extensions::FeatureSwitch::ScopedOverride> enable_redesign_;
  scoped_ptr<MockComponentToolbarActionsFactory> mock_actions_factory_;

  DISALLOW_COPY_AND_ASSIGN(ComponentToolbarActionsBrowserTest);
};

// Test that Component Toolbar Actions appear in the browser actions container
// and can receive click events properly.
IN_PROC_BROWSER_TEST_F(ComponentToolbarActionsBrowserTest,
                       ComponentToolbarActionsShowUpAndRespondToClicks) {
  BrowserActionsContainer* browser_actions_container =
      BrowserView::GetBrowserViewForBrowser(browser())
          ->toolbar()->browser_actions();

  // There should be only one component action view.
  ASSERT_EQ(1u, browser_actions_container->num_toolbar_actions());

  ToolbarActionView* view =
      browser_actions_container->GetToolbarActionViewAt(0u);
  ASSERT_EQ(kMockId, view->view_controller()->GetId());
  MockComponentAction* mock_component_action =
      static_cast<MockComponentAction*>(view->view_controller());

  // Test that clicking on the component action works.
  EXPECT_EQ(0u, mock_component_action->click_count());
  view->Activate();
  EXPECT_EQ(1u, mock_component_action->click_count());
}