summaryrefslogtreecommitdiffstats
path: root/ui/app_list/views/speech_view_unittest.cc
blob: 1c49faad4ab767c34e61de1ec3d36b0cdaa9c82e (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
// 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 "ui/app_list/views/speech_view.h"

#include "ui/app_list/test/app_list_test_view_delegate.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/test/widget_test.h"

namespace app_list {
namespace test {

class SpeechViewTest : public views::test::WidgetTest,
                       public AppListTestViewDelegate {
 public:
  SpeechViewTest() {}
  virtual ~SpeechViewTest() {}

  // Overridden from testing::Test:
  virtual void SetUp() OVERRIDE {
    views::test::WidgetTest::SetUp();
    widget_ = CreateTopLevelPlatformWidget();
    widget_->SetBounds(gfx::Rect(0, 0, 300, 200));
    view_ = new SpeechView(&view_delegate_);
    widget_->GetContentsView()->AddChildView(view_);
    view_->SetBoundsRect(widget_->GetContentsView()->GetLocalBounds());
  }

  virtual void TearDown() OVERRIDE {
    widget_->CloseNow();
    views::test::WidgetTest::TearDown();
  }

 protected:
  SpeechView* view() { return view_; }
  views::Widget* widget() { return widget_; }

  int GetToggleSpeechRecognitionCountAndReset() {
    return view_delegate_.GetToggleSpeechRecognitionCountAndReset();
  }

 private:
  AppListTestViewDelegate view_delegate_;
  views::Widget* widget_;
  SpeechView* view_;

  DISALLOW_COPY_AND_ASSIGN(SpeechViewTest);
};

// Tests that clicking within the circular hit-test mask of MicButton invokes
// SpeechView::ToggleSpeechRecognition() and clicking outside of the
// hit-test mask does not.
TEST_F(SpeechViewTest, ClickMicButton) {
  EXPECT_EQ(0, GetToggleSpeechRecognitionCountAndReset());
  gfx::Rect screen_bounds(view()->mic_button()->GetBoundsInScreen());

  // Simulate a mouse click in the center of the MicButton.
  ui::MouseEvent press(ui::ET_MOUSE_PRESSED,
                       screen_bounds.CenterPoint(),
                       screen_bounds.CenterPoint(),
                       ui::EF_LEFT_MOUSE_BUTTON,
                       0);
  ui::MouseEvent release(ui::ET_MOUSE_RELEASED,
                         screen_bounds.CenterPoint(),
                         screen_bounds.CenterPoint(),
                         ui::EF_LEFT_MOUSE_BUTTON,
                         0);
  widget()->OnMouseEvent(&press);
  widget()->OnMouseEvent(&release);
  EXPECT_EQ(1, GetToggleSpeechRecognitionCountAndReset());

  // Simulate a mouse click in the bottom right-hand corner of the
  // MicButton's view bounds (which would fall outside of its
  // circular hit-test mask).
  gfx::Point bottom_right(screen_bounds.right() - 1,
                          screen_bounds.bottom() - 2);
  press = ui::MouseEvent(ui::ET_MOUSE_PRESSED,
                         bottom_right,
                         bottom_right,
                         ui::EF_LEFT_MOUSE_BUTTON,
                         0);
  release = ui::MouseEvent(ui::ET_MOUSE_RELEASED,
                           bottom_right,
                           bottom_right,
                           ui::EF_LEFT_MOUSE_BUTTON,
                           0);
  widget()->OnMouseEvent(&press);
  widget()->OnMouseEvent(&release);
  EXPECT_EQ(0, GetToggleSpeechRecognitionCountAndReset());
}

}  // namespace test
}  // namespace app_list