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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/nearby_share/nearby_share_dialog_ui.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/nearby_sharing/common/nearby_share_features.h"
#include "chrome/browser/sharesheet/sharesheet_controller.h"
#include "chrome/browser/sharesheet/sharesheet_types.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chromeos/components/sharesheet/constants.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "url/gurl.h"
namespace {
// Keep in sync with
// chrome/browser/resources/chromeos/nearby_share/shared/types.ts
enum class CloseReason {
kUnknown = 0,
kTransferStarted = 1,
kTransferSucceeded = 2,
kCancelled = 3,
kRejected = 4
};
class TestSharesheetController : public sharesheet::SharesheetController {
public:
// sharesheet::SharesheetController
void SetBubbleSize(int width, int height) override {}
void CloseBubble(::sharesheet::SharesheetResult result) override {
last_result = result;
}
bool IsBubbleVisible() const override { return !last_result; }
std::optional<::sharesheet::SharesheetResult> last_result;
};
class NearbyShareDialogUITest : public InProcessBrowserTest {
protected:
content::WebContents* GetWebContentsForNearbyShareHost() const {
GURL kUrl(content::GetWebUIURL(chrome::kChromeUINearbyShareHost));
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), kUrl));
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_TRUE(web_contents);
EXPECT_EQ(kUrl, web_contents->GetLastCommittedURL());
EXPECT_FALSE(web_contents->IsCrashed());
return web_contents;
}
TestSharesheetController sharesheet_controller_;
};
std::string BuildCloseScript(CloseReason reason) {
return base::StringPrintf("chrome.send('close',[%d]);",
static_cast<int>(reason));
}
} // namespace
IN_PROC_BROWSER_TEST_F(NearbyShareDialogUITest, RendersComponent) {
content::WebContents* web_contents = GetWebContentsForNearbyShareHost();
// Assert that we render the nearby-share-app component.
EXPECT_EQ(1, content::EvalJs(
web_contents,
"document.getElementsByTagName('nearby-share-app').length"));
}
IN_PROC_BROWSER_TEST_F(NearbyShareDialogUITest,
SharesheetControllerGetsCalledOnClose) {
content::WebContents* web_contents = GetWebContentsForNearbyShareHost();
auto* webui = web_contents->GetWebUI();
ASSERT_TRUE(webui);
// Add a test observer and verify it gets called when 'close' is sent.
auto* nearby_ui =
webui->GetController()->GetAs<nearby_share::NearbyShareDialogUI>();
ASSERT_TRUE(nearby_ui);
// Calling 'close' before a Sharesheet controller is registered via
// |SetSharesheetController| does not result in a crash.
std::string script = BuildCloseScript(CloseReason::kCancelled);
EXPECT_TRUE(content::ExecJs(web_contents, script));
EXPECT_FALSE(sharesheet_controller_.last_result);
// The Sharesheet controller gets called on 'close' if it's been registered.
nearby_ui->SetSharesheetController(&sharesheet_controller_);
EXPECT_TRUE(content::ExecJs(web_contents, script));
EXPECT_EQ(::sharesheet::SharesheetResult::kCancel,
sharesheet_controller_.last_result);
// Any subsequent calls to 'close' do not call the Sharesheet controller,
// since that would result in a crash.
sharesheet_controller_.last_result.reset();
EXPECT_TRUE(content::ExecJs(web_contents, script));
EXPECT_FALSE(sharesheet_controller_.last_result);
}
IN_PROC_BROWSER_TEST_F(NearbyShareDialogUITest, CloseBubbleResults) {
for (CloseReason reason :
{CloseReason::kUnknown, CloseReason::kTransferStarted,
CloseReason::kTransferSucceeded, CloseReason::kCancelled,
CloseReason::kRejected}) {
content::WebContents* web_contents = GetWebContentsForNearbyShareHost();
auto* nearby_ui = web_contents->GetWebUI()
->GetController()
->GetAs<nearby_share::NearbyShareDialogUI>();
sharesheet_controller_.last_result.reset();
nearby_ui->SetSharesheetController(&sharesheet_controller_);
EXPECT_TRUE(content::ExecJs(web_contents, BuildCloseScript(reason)));
// Verify that the page-closed reason is translated into the correct
// SharesheetResult and passed into CloseBubble().
switch (reason) {
case CloseReason::kTransferStarted:
case CloseReason::kTransferSucceeded:
EXPECT_EQ(sharesheet::SharesheetResult::kSuccess,
sharesheet_controller_.last_result);
break;
case CloseReason::kUnknown:
case CloseReason::kCancelled:
case CloseReason::kRejected:
EXPECT_EQ(sharesheet::SharesheetResult::kCancel,
sharesheet_controller_.last_result);
break;
}
}
}
|