Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / global_error / global_error_service_browsertest.cc
blobee09eea8f6980969b90a8f25745236c3ed7b14cd
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/global_error/global_error_service.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/global_error/global_error.h"
10 #include "chrome/browser/ui/global_error/global_error_bubble_view_base.h"
11 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "content/public/test/test_utils.h"
15 namespace {
17 // An error that has a bubble view.
18 class BubbleViewError : public GlobalErrorWithStandardBubble {
19 public:
20 BubbleViewError() : bubble_view_close_count_(0) { }
22 int bubble_view_close_count() { return bubble_view_close_count_; }
24 bool HasMenuItem() override { return false; }
25 int MenuItemCommandID() override {
26 ADD_FAILURE();
27 return 0;
29 base::string16 MenuItemLabel() override {
30 ADD_FAILURE();
31 return base::string16();
33 void ExecuteMenuItem(Browser* browser) override { ADD_FAILURE(); }
35 bool HasBubbleView() override { return true; }
36 base::string16 GetBubbleViewTitle() override { return base::string16(); }
37 std::vector<base::string16> GetBubbleViewMessages() override {
38 return std::vector<base::string16>();
40 base::string16 GetBubbleViewAcceptButtonLabel() override {
41 return base::string16();
43 base::string16 GetBubbleViewCancelButtonLabel() override {
44 return base::string16();
46 void OnBubbleViewDidClose(Browser* browser) override {
47 EXPECT_TRUE(browser);
48 ++bubble_view_close_count_;
50 void BubbleViewAcceptButtonPressed(Browser* browser) override {}
51 void BubbleViewCancelButtonPressed(Browser* browser) override {}
53 private:
54 int bubble_view_close_count_;
56 DISALLOW_COPY_AND_ASSIGN(BubbleViewError);
59 } // namespace
61 class GlobalErrorServiceBrowserTest : public InProcessBrowserTest {
64 // Test that showing a error with a bubble view works.
65 IN_PROC_BROWSER_TEST_F(GlobalErrorServiceBrowserTest, ShowBubbleView) {
66 // This will be deleted by the GlobalErrorService.
67 BubbleViewError* error = new BubbleViewError;
69 GlobalErrorService* service =
70 GlobalErrorServiceFactory::GetForProfile(browser()->profile());
71 service->AddGlobalError(error);
73 EXPECT_EQ(error, service->GetFirstGlobalErrorWithBubbleView());
74 EXPECT_FALSE(error->HasShownBubbleView());
75 EXPECT_EQ(0, error->bubble_view_close_count());
77 // Creating a second browser window should show the bubble view.
78 CreateBrowser(browser()->profile());
79 EXPECT_EQ(NULL, service->GetFirstGlobalErrorWithBubbleView());
80 EXPECT_TRUE(error->HasShownBubbleView());
81 EXPECT_EQ(0, error->bubble_view_close_count());
84 // Test that GlobalErrorBubbleViewBase::CloseBubbleView correctly closes the
85 // bubble view.
86 IN_PROC_BROWSER_TEST_F(GlobalErrorServiceBrowserTest, CloseBubbleView) {
87 // This will be deleted by the GlobalErrorService.
88 BubbleViewError* error = new BubbleViewError;
90 GlobalErrorService* service =
91 GlobalErrorServiceFactory::GetForProfile(browser()->profile());
92 service->AddGlobalError(error);
94 EXPECT_EQ(error, service->GetFirstGlobalErrorWithBubbleView());
95 EXPECT_FALSE(error->HasShownBubbleView());
96 EXPECT_EQ(0, error->bubble_view_close_count());
98 // Creating a second browser window should show the bubble view.
99 CreateBrowser(browser()->profile());
100 EXPECT_EQ(NULL, service->GetFirstGlobalErrorWithBubbleView());
101 EXPECT_TRUE(error->HasShownBubbleView());
102 EXPECT_EQ(0, error->bubble_view_close_count());
104 // Explicitly close the bubble view.
105 EXPECT_TRUE(error->GetBubbleView());
106 error->GetBubbleView()->CloseBubbleView();
107 content::RunAllPendingInMessageLoop();
108 EXPECT_EQ(1, error->bubble_view_close_count());
111 // Test that bubble is silently dismissed if it is showing when the GlobalError
112 // instance is removed from the profile.
113 #if defined(OS_WIN) || defined(OS_LINUX)
114 // http://crbug.com/396473
115 #define MAYBE_BubbleViewDismissedOnRemove DISABLED_BubbleViewDismissedOnRemove
116 #else
117 #define MAYBE_BubbleViewDismissedOnRemove BubbleViewDismissedOnRemove
118 #endif
119 IN_PROC_BROWSER_TEST_F(GlobalErrorServiceBrowserTest,
120 MAYBE_BubbleViewDismissedOnRemove) {
121 scoped_ptr<BubbleViewError> error(new BubbleViewError);
123 GlobalErrorService* service =
124 GlobalErrorServiceFactory::GetForProfile(browser()->profile());
125 service->AddGlobalError(error.get());
127 EXPECT_EQ(error.get(), service->GetFirstGlobalErrorWithBubbleView());
128 error->ShowBubbleView(browser());
129 content::RunAllPendingInMessageLoop();
130 EXPECT_TRUE(error->HasShownBubbleView());
131 EXPECT_EQ(0, error->bubble_view_close_count());
133 // Removing |error| from profile should dismiss the bubble view without
134 // calling |error->BubbleViewDidClose|.
135 service->RemoveGlobalError(error.get());
136 content::RunAllPendingInMessageLoop();
137 EXPECT_EQ(1, error->bubble_view_close_count());
138 // |error| is no longer owned by service and will be deleted.