Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7278)

Unified Diff: chrome/browser/speech/speech_input_bubble_controller_unittest.cc

Issue 3352018: Show error messages in speech bubble allowing user to retry as well. (Closed)
Patch Set: Address joth's comments. Created 10 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/speech/speech_input_bubble_controller_unittest.cc
diff --git a/chrome/browser/speech/speech_input_bubble_controller_unittest.cc b/chrome/browser/speech/speech_input_bubble_controller_unittest.cc
index b2e10c6008a263607331af0316b7c1ee50cadda2..59eac15d424cd6fbda738e0c6cfac87087d8083b 100644
--- a/chrome/browser/speech/speech_input_bubble_controller_unittest.cc
+++ b/chrome/browser/speech/speech_input_bubble_controller_unittest.cc
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/utf_string_conversions.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/speech/speech_input_bubble_controller.h"
#include "gfx/rect.h"
@@ -11,11 +12,12 @@ namespace speech_input {
// A mock bubble class which fakes a focus change or recognition cancel by the
// user and closing of the info bubble.
-class MockSpeechInputBubble : public SpeechInputBubble {
+class MockSpeechInputBubble : public SpeechInputBubbleBase {
public:
enum BubbleType {
BUBBLE_TEST_FOCUS_CHANGED,
- BUBBLE_TEST_RECOGNITION_CANCELLED
+ BUBBLE_TEST_CLICK_CANCEL,
+ BUBBLE_TEST_CLICK_TRY_AGAIN,
};
MockSpeechInputBubble(TabContents*, Delegate* delegate, const gfx::Rect&) {
@@ -24,17 +26,29 @@ class MockSpeechInputBubble : public SpeechInputBubble {
}
static void InvokeDelegate(Delegate* delegate) {
- if (type_ == BUBBLE_TEST_FOCUS_CHANGED)
- delegate->InfoBubbleClosed();
- else
- delegate->RecognitionCancelled();
+ switch (type_) {
+ case BUBBLE_TEST_FOCUS_CHANGED:
+ delegate->InfoBubbleFocusChanged();
+ break;
+ case BUBBLE_TEST_CLICK_CANCEL:
+ delegate->InfoBubbleButtonClicked(SpeechInputBubble::BUTTON_CANCEL);
+ break;
+ case BUBBLE_TEST_CLICK_TRY_AGAIN:
+ delegate->InfoBubbleButtonClicked(SpeechInputBubble::BUTTON_TRY_AGAIN);
+ break;
+ }
}
static void set_type(BubbleType type) {
type_ = type;
}
+ static BubbleType type() {
+ return type_;
+ }
- virtual void SetRecognizingMode() {}
+ virtual void Show() {}
+ virtual void Hide() {}
+ virtual void UpdateLayout() {}
private:
static BubbleType type_;
@@ -49,18 +63,32 @@ class SpeechInputBubbleControllerTest
: io_loop_(MessageLoop::TYPE_IO),
ui_thread_(ChromeThread::UI), // constructs a new thread and loop
io_thread_(ChromeThread::IO, &io_loop_), // resuses main thread loop
- recognition_cancelled_(false),
- focus_changed_(false) {
+ cancel_clicked_(false),
+ try_again_clicked_(false),
+ focus_changed_(false),
+ controller_(ALLOW_THIS_IN_INITIALIZER_LIST(
+ new SpeechInputBubbleController(this))) {
+ EXPECT_EQ(NULL, test_fixture_);
+ test_fixture_ = this;
+ }
+
+ ~SpeechInputBubbleControllerTest() {
+ test_fixture_ = NULL;
}
// SpeechInputBubbleControllerDelegate methods.
- virtual void RecognitionCancelled(int caller_id) {
+ virtual void InfoBubbleButtonClicked(int caller_id,
+ SpeechInputBubble::Button button) {
EXPECT_TRUE(ChromeThread::CurrentlyOn(ChromeThread::IO));
- recognition_cancelled_ = true;
+ if (button == SpeechInputBubble::BUTTON_CANCEL) {
+ cancel_clicked_ = true;
+ } else if (button == SpeechInputBubble::BUTTON_TRY_AGAIN) {
+ try_again_clicked_ = true;
+ }
MessageLoop::current()->Quit();
}
- virtual void SpeechInputFocusChanged(int caller_id) {
+ virtual void InfoBubbleFocusChanged(int caller_id) {
EXPECT_TRUE(ChromeThread::CurrentlyOn(ChromeThread::IO));
focus_changed_ = true;
MessageLoop::current()->Quit();
@@ -78,10 +106,25 @@ class SpeechInputBubbleControllerTest
ui_thread_.Stop();
}
+ static void ActivateBubble() {
+ if (MockSpeechInputBubble::type() ==
+ MockSpeechInputBubble::BUBBLE_TEST_FOCUS_CHANGED) {
+ test_fixture_->controller_->SetBubbleRecordingMode(kBubbleCallerId);
+ } else {
+ test_fixture_->controller_->SetBubbleMessage(kBubbleCallerId,
+ ASCIIToUTF16("Test"));
+ }
+ }
+
static SpeechInputBubble* CreateBubble(TabContents* tab_contents,
SpeechInputBubble::Delegate* delegate,
const gfx::Rect& element_rect) {
EXPECT_TRUE(ChromeThread::CurrentlyOn(ChromeThread::UI));
+ // Set up to activate the bubble soon after it gets created, since we test
+ // events sent by the bubble and those are handled only when the bubble is
+ // active.
+ MessageLoop::current()->PostTask(FROM_HERE,
+ NewRunnableFunction(&ActivateBubble));
return new MockSpeechInputBubble(tab_contents, delegate, element_rect);
}
@@ -91,10 +134,20 @@ class SpeechInputBubbleControllerTest
MessageLoop io_loop_;
ChromeThread ui_thread_;
ChromeThread io_thread_;
- bool recognition_cancelled_;
+ bool cancel_clicked_;
+ bool try_again_clicked_;
bool focus_changed_;
+ scoped_refptr<SpeechInputBubbleController> controller_;
+
+ static const int kBubbleCallerId;
+ static SpeechInputBubbleControllerTest* test_fixture_;
};
+SpeechInputBubbleControllerTest*
+SpeechInputBubbleControllerTest::test_fixture_ = NULL;
+
+const int SpeechInputBubbleControllerTest::kBubbleCallerId = 1;
+
MockSpeechInputBubble::BubbleType MockSpeechInputBubble::type_ =
MockSpeechInputBubble::BUBBLE_TEST_FOCUS_CHANGED;
@@ -111,28 +164,40 @@ TEST_F(SpeechInputBubbleControllerTest, MAYBE_TestFocusChanged) {
MockSpeechInputBubble::set_type(
MockSpeechInputBubble::BUBBLE_TEST_FOCUS_CHANGED);
- scoped_refptr<SpeechInputBubbleController> controller(
- new SpeechInputBubbleController(this));
-
- controller->CreateBubble(0, 1, 1, gfx::Rect(1, 1));
+ controller_->CreateBubble(kBubbleCallerId, 1, 1, gfx::Rect(1, 1));
MessageLoop::current()->Run();
EXPECT_TRUE(focus_changed_);
- EXPECT_FALSE(recognition_cancelled_);
+ EXPECT_FALSE(cancel_clicked_);
+ EXPECT_FALSE(try_again_clicked_);
+ controller_->CloseBubble(kBubbleCallerId);
}
// Test that the speech bubble UI gets created in the UI thread and that the
// recognition cancelled callback comes back in the IO thread.
TEST_F(SpeechInputBubbleControllerTest, TestRecognitionCancelled) {
MockSpeechInputBubble::set_type(
- MockSpeechInputBubble::BUBBLE_TEST_RECOGNITION_CANCELLED);
+ MockSpeechInputBubble::BUBBLE_TEST_CLICK_CANCEL);
- scoped_refptr<SpeechInputBubbleController> controller(
- new SpeechInputBubbleController(this));
+ controller_->CreateBubble(kBubbleCallerId, 1, 1, gfx::Rect(1, 1));
+ MessageLoop::current()->Run();
+ EXPECT_TRUE(cancel_clicked_);
+ EXPECT_FALSE(try_again_clicked_);
+ EXPECT_FALSE(focus_changed_);
+ controller_->CloseBubble(kBubbleCallerId);
+}
+
+// Test that the speech bubble UI gets created in the UI thread and that the
+// try-again button click event comes back in the IO thread.
+TEST_F(SpeechInputBubbleControllerTest, TestTryAgainClicked) {
+ MockSpeechInputBubble::set_type(
+ MockSpeechInputBubble::BUBBLE_TEST_CLICK_TRY_AGAIN);
- controller->CreateBubble(0, 1, 1, gfx::Rect(1, 1));
+ controller_->CreateBubble(kBubbleCallerId, 1, 1, gfx::Rect(1, 1));
MessageLoop::current()->Run();
- EXPECT_TRUE(recognition_cancelled_);
+ EXPECT_FALSE(cancel_clicked_);
+ EXPECT_TRUE(try_again_clicked_);
EXPECT_FALSE(focus_changed_);
+ controller_->CloseBubble(kBubbleCallerId);
}
} // namespace speech_input

Powered by Google App Engine
This is Rietveld 408576698