summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/translate/translate_bubble_controller_unittest.mm
blob: f72588b1b49d9a4c46e526b6c537adad9af44e4a (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
// 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 "chrome/browser/ui/cocoa/translate/translate_bubble_controller.h"

#include "base/message_loop/message_loop.h"
#include "base/test/histogram_tester.h"
#import "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#import "chrome/browser/ui/cocoa/browser_window_controller.h"
#import "chrome/browser/ui/cocoa/cocoa_profile_test.h"
#import "chrome/browser/ui/cocoa/info_bubble_window.h"
#include "chrome/browser/ui/cocoa/run_loop_testing.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/site_instance.h"

@implementation BrowserWindowController (ForTesting)

- (TranslateBubbleController*)translateBubbleController{
  return translateBubbleController_;
}

@end

class TranslateBubbleControllerTest : public CocoaProfileTest {
 public:
  void SetUp() override {
    CocoaProfileTest::SetUp();
    site_instance_ = content::SiteInstance::Create(profile());

    NSWindow* nativeWindow = browser()->window()->GetNativeWindow();
    bwc_ =
        [BrowserWindowController browserWindowControllerForWindow:nativeWindow];
    web_contents_ = AppendToTabStrip();
  }

  content::WebContents* AppendToTabStrip() {
    content::WebContents* web_contents = content::WebContents::Create(
        content::WebContents::CreateParams(profile(), site_instance_.get()));
    browser()->tab_strip_model()->AppendWebContents(
        web_contents, /*foreground=*/true);
    return web_contents;
  }

  BrowserWindowController* bwc() { return bwc_; }
  TranslateBubbleController* bubble() {
    return [bwc() translateBubbleController];
  }

  void ShowBubble() {
    ASSERT_FALSE(bubble());
    translate::TranslateStep step = translate::TRANSLATE_STEP_BEFORE_TRANSLATE;

    [bwc_ showTranslateBubbleForWebContents:web_contents_
                                       step:step
                                  errorType:translate::TranslateErrors::NONE];

    // Ensure that there are no closing animations.
    InfoBubbleWindow* window = (InfoBubbleWindow*)[bubble() window];
    [window setAllowedAnimations:info_bubble::kAnimateNone];
  }

  void CloseBubble() {
    [bubble() close];
    chrome::testing::NSRunLoopRunAllPending();
  }

 private:
  scoped_refptr<content::SiteInstance> site_instance_;
  BrowserWindowController* bwc_;
  content::WebContents* web_contents_;
};

TEST_F(TranslateBubbleControllerTest, ShowAndClose) {
  EXPECT_FALSE(bubble());

  ShowBubble();
  EXPECT_TRUE(bubble());

  CloseBubble();
  EXPECT_FALSE(bubble());
}

TEST_F(TranslateBubbleControllerTest, CloseRegistersDecline) {
  const char kDeclineTranslateDismissUI[] =
      "Translate.DeclineTranslateDismissUI";
  const char kDeclineTranslate[] = "Translate.DeclineTranslate";

  // A simple close without any interactions registers as a dismissal.
  {
    base::HistogramTester histogram_tester;
    ShowBubble();
    CloseBubble();
    histogram_tester.ExpectTotalCount(kDeclineTranslateDismissUI, 1);
    histogram_tester.ExpectTotalCount(kDeclineTranslate, 0);
  }

  // A close while pressing e.g. 'Nope', registers as decline.
  {
    base::HistogramTester histogram_tester;
    ShowBubble();
    [bubble() handleDenialPopUpButtonNopeSelected];
    CloseBubble();
    histogram_tester.ExpectTotalCount(kDeclineTranslateDismissUI, 0);
    histogram_tester.ExpectTotalCount(kDeclineTranslate, 1);
  }
}