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.
4
5 #include "chrome/browser/ui/global_error/global_error_service.h"
6
7 #include <memory>
8
9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "build/build_config.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/global_error/global_error.h"
15 #include "chrome/browser/ui/global_error/global_error_bubble_view_base.h"
16 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "content/public/test/browser_test.h"
19 #include "content/public/test/test_utils.h"
20
21 namespace {
22
23 // An error that has a bubble view.
24 class BubbleViewError : public GlobalErrorWithStandardBubble {
25 public:
BubbleViewError()26 BubbleViewError() : bubble_view_close_count_(0) { }
27
bubble_view_close_count()28 int bubble_view_close_count() { return bubble_view_close_count_; }
29
HasMenuItem()30 bool HasMenuItem() override { return false; }
MenuItemCommandID()31 int MenuItemCommandID() override {
32 ADD_FAILURE();
33 return 0;
34 }
MenuItemLabel()35 base::string16 MenuItemLabel() override {
36 ADD_FAILURE();
37 return base::string16();
38 }
ExecuteMenuItem(Browser * browser)39 void ExecuteMenuItem(Browser* browser) override { ADD_FAILURE(); }
40
HasBubbleView()41 bool HasBubbleView() override { return true; }
GetBubbleViewTitle()42 base::string16 GetBubbleViewTitle() override { return base::string16(); }
GetBubbleViewMessages()43 std::vector<base::string16> GetBubbleViewMessages() override {
44 return std::vector<base::string16>();
45 }
GetBubbleViewAcceptButtonLabel()46 base::string16 GetBubbleViewAcceptButtonLabel() override {
47 return base::ASCIIToUTF16("OK");
48 }
GetBubbleViewCancelButtonLabel()49 base::string16 GetBubbleViewCancelButtonLabel() override {
50 return base::ASCIIToUTF16("Cancel");
51 }
OnBubbleViewDidClose(Browser * browser)52 void OnBubbleViewDidClose(Browser* browser) override {
53 EXPECT_TRUE(browser);
54 ++bubble_view_close_count_;
55 }
BubbleViewAcceptButtonPressed(Browser * browser)56 void BubbleViewAcceptButtonPressed(Browser* browser) override {}
BubbleViewCancelButtonPressed(Browser * browser)57 void BubbleViewCancelButtonPressed(Browser* browser) override {}
58
59 private:
60 int bubble_view_close_count_;
61
62 DISALLOW_COPY_AND_ASSIGN(BubbleViewError);
63 };
64
65 } // namespace
66
67 class GlobalErrorServiceBrowserTest : public InProcessBrowserTest {
68 };
69
70 // Test that showing a error with a bubble view works.
IN_PROC_BROWSER_TEST_F(GlobalErrorServiceBrowserTest,ShowBubbleView)71 IN_PROC_BROWSER_TEST_F(GlobalErrorServiceBrowserTest, ShowBubbleView) {
72 // This will be deleted by the GlobalErrorService.
73 BubbleViewError* error = new BubbleViewError;
74
75 GlobalErrorService* service =
76 GlobalErrorServiceFactory::GetForProfile(browser()->profile());
77 service->AddGlobalError(base::WrapUnique(error));
78
79 EXPECT_EQ(error, service->GetFirstGlobalErrorWithBubbleView());
80 EXPECT_FALSE(error->HasShownBubbleView());
81 EXPECT_EQ(0, error->bubble_view_close_count());
82
83 // Creating a second browser window should show the bubble view.
84 CreateBrowser(browser()->profile());
85 EXPECT_EQ(NULL, service->GetFirstGlobalErrorWithBubbleView());
86 EXPECT_TRUE(error->HasShownBubbleView());
87 EXPECT_EQ(0, error->bubble_view_close_count());
88 }
89
90 // Test that GlobalErrorBubbleViewBase::CloseBubbleView correctly closes the
91 // bubble view.
IN_PROC_BROWSER_TEST_F(GlobalErrorServiceBrowserTest,CloseBubbleView)92 IN_PROC_BROWSER_TEST_F(GlobalErrorServiceBrowserTest, CloseBubbleView) {
93 // This will be deleted by the GlobalErrorService.
94 BubbleViewError* error = new BubbleViewError;
95
96 GlobalErrorService* service =
97 GlobalErrorServiceFactory::GetForProfile(browser()->profile());
98 service->AddGlobalError(base::WrapUnique(error));
99
100 EXPECT_EQ(error, service->GetFirstGlobalErrorWithBubbleView());
101 EXPECT_FALSE(error->HasShownBubbleView());
102 EXPECT_EQ(0, error->bubble_view_close_count());
103
104 // Creating a second browser window should show the bubble view.
105 CreateBrowser(browser()->profile());
106 EXPECT_EQ(NULL, service->GetFirstGlobalErrorWithBubbleView());
107 EXPECT_TRUE(error->HasShownBubbleView());
108 EXPECT_EQ(0, error->bubble_view_close_count());
109
110 // Explicitly close the bubble view.
111 EXPECT_TRUE(error->GetBubbleView());
112 error->GetBubbleView()->CloseBubbleView();
113 content::RunAllPendingInMessageLoop();
114 EXPECT_EQ(1, error->bubble_view_close_count());
115 }
116
117 // Test that bubble is silently dismissed if it is showing when the GlobalError
118 // instance is removed from the profile.
119 //
120 // This uses the deprecated "unowned" API to the GlobalErrorService to maintain
121 // coverage. When those calls are eventually removed (http://crbug.com/673578)
122 // these uses should be switched to the non-deprecated API.
IN_PROC_BROWSER_TEST_F(GlobalErrorServiceBrowserTest,BubbleViewDismissedOnRemove)123 IN_PROC_BROWSER_TEST_F(GlobalErrorServiceBrowserTest,
124 BubbleViewDismissedOnRemove) {
125 std::unique_ptr<BubbleViewError> error(new BubbleViewError);
126
127 GlobalErrorService* service =
128 GlobalErrorServiceFactory::GetForProfile(browser()->profile());
129 service->AddUnownedGlobalError(error.get());
130
131 EXPECT_EQ(error.get(), service->GetFirstGlobalErrorWithBubbleView());
132 error->ShowBubbleView(browser());
133 content::RunAllPendingInMessageLoop();
134 EXPECT_TRUE(error->HasShownBubbleView());
135 EXPECT_EQ(0, error->bubble_view_close_count());
136
137 // Removing |error| from profile should dismiss the bubble view without
138 // calling |error->BubbleViewDidClose|.
139 service->RemoveUnownedGlobalError(error.get());
140 content::RunAllPendingInMessageLoop();
141 EXPECT_EQ(1, error->bubble_view_close_count());
142 }
143