Skip to content

Commit

Permalink
Add tests for Widevine models, pref and exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
bbondy committed Jun 27, 2018
1 parent 9cc7479 commit e35fcf4
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 1 deletion.
3 changes: 3 additions & 0 deletions browser/brave_profile_prefs_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/common/pref_names.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/pref_names.h"
Expand All @@ -14,4 +15,6 @@ using BraveProfilePrefsBrowserTest = InProcessBrowserTest;
IN_PROC_BROWSER_TEST_F(BraveProfilePrefsBrowserTest, DownloadPromptDefault) {
EXPECT_TRUE(
browser()->profile()->GetPrefs()->GetBoolean(prefs::kPromptForDownload));
EXPECT_FALSE(
browser()->profile()->GetPrefs()->GetBoolean(kWidevineOptedIn));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "base/path_service.h"
#include "brave/browser/ui/content_settings/brave_content_setting_bubble_model.h"
#include "brave/browser/ui/content_settings/brave_widevine_blocked_image_model.h"
#include "brave/browser/ui/content_settings/brave_widevine_content_setting_bubble_model.h"
#include "brave/common/brave_paths.h"
#include "brave/common/pref_names.h"
#include "brave/common/url_constants.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_content_setting_bubble_model_delegate.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test_utils.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/test_navigation_observer.h"
#include "net/dns/mock_host_resolver.h"

using content::WebContents;
using ImageType = ContentSettingImageModel::ImageType;

class BraveWidevineBlockedImageModelBrowserTest : public InProcessBrowserTest {
public:
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1");

brave::RegisterPathProvider();
base::FilePath test_data_dir;
PathService::Get(brave::DIR_TEST_DATA, &test_data_dir);
embedded_test_server()->ServeFilesFromDirectory(test_data_dir);

ASSERT_TRUE(embedded_test_server()->Start());
}

content::WebContents* active_contents() {
return browser()->tab_strip_model()->GetActiveWebContents();
}

bool NavigateToURLUntilLoadStop(const GURL& url) {
ui_test_utils::NavigateToURL(browser(), url);
return WaitForLoadStop(active_contents());
}

std::unique_ptr<BraveWidevineBlockedImageModel> CreateModel() {
return std::make_unique<BraveWidevineBlockedImageModel>(
BraveWidevineBlockedImageModel::ImageType::PLUGINS,
CONTENT_SETTINGS_TYPE_PLUGINS);
}
};

// Tests that every model creates a valid bubble.
IN_PROC_BROWSER_TEST_F(BraveWidevineBlockedImageModelBrowserTest, CreateBubbleModel) {
WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
Profile* profile = browser()->profile();
auto model = CreateModel();
std::unique_ptr<ContentSettingBubbleModel> bubble(
model->CreateBubbleModel(nullptr, web_contents, profile));
ContentSettingSimpleBubbleModel* simple_bubble =
bubble->AsSimpleBubbleModel();
ASSERT_TRUE(simple_bubble);
EXPECT_EQ(static_cast<ContentSettingSimpleImageModel*>(model.get())
->content_type(), simple_bubble->content_type());
EXPECT_EQ(ImageType::PLUGINS, model->image_type());
}

// Tests that we correctly remember for which WebContents the animation has run,
// and thus we should not run it again.
IN_PROC_BROWSER_TEST_F(BraveWidevineBlockedImageModelBrowserTest,
ShouldRunAnimation) {
WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
auto model = CreateModel();
EXPECT_TRUE(model->ShouldRunAnimation(web_contents));
model->SetAnimationHasRun(web_contents);
EXPECT_FALSE(model->ShouldRunAnimation(web_contents));

// The animation has run for the current WebContents, but not for any other.
Profile* profile = browser()->profile();
WebContents::CreateParams create_params(profile);
WebContents* other_web_contents = WebContents::Create(create_params);
browser()->tab_strip_model()->AppendWebContents(other_web_contents, true);
EXPECT_TRUE(model->ShouldRunAnimation(other_web_contents));
}

// Tests that we go to the correct link when learn more is clicked in the bubble
IN_PROC_BROWSER_TEST_F(BraveWidevineBlockedImageModelBrowserTest,
LearnMoreLinkClicked) {
WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();

auto model = CreateModel();
Profile* profile = browser()->profile();
std::unique_ptr<ContentSettingBubbleModel> bubble(model->CreateBubbleModel(
browser()->content_setting_bubble_model_delegate(), web_contents,
profile));

content::TestNavigationObserver observer(nullptr);
observer.StartWatchingNewWebContents();
bubble->OnLearnMoreClicked();
observer.Wait();

std::string link_value(kWidevineTOS);
EXPECT_EQ(link_value, observer.last_navigation_url().spec());
}

// Tests that the content setting model shows and that runninng plugins changes the
// opt in setting.
IN_PROC_BROWSER_TEST_F(BraveWidevineBlockedImageModelBrowserTest,
RunPluginsOnPageClicked) {
GURL url = embedded_test_server()->GetURL("www.netflix.com", "/blank.html");
NavigateToURLUntilLoadStop(url);

auto model = CreateModel();
Profile* profile = browser()->profile();
WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
std::unique_ptr<BraveWidevineContentSettingPluginBubbleModel> bubble(
(BraveWidevineContentSettingPluginBubbleModel*)
model->CreateBubbleModel(
browser()->content_setting_bubble_model_delegate(), web_contents,
profile));

PrefService* prefs = ProfileManager::GetActiveUserProfile()->GetPrefs();

// Before we allow, opted in should be false
ASSERT_FALSE(prefs->GetBoolean(kWidevineOptedIn));

bubble->RunPluginsOnPage();

// After we allow, opted in pref should be true
ASSERT_TRUE(prefs->GetBoolean(kWidevineOptedIn));
}

// Tests that if the user already installed and opted in, that we don't
// show the content setting model.
IN_PROC_BROWSER_TEST_F(BraveWidevineBlockedImageModelBrowserTest,
RunPluginsOnPageClickedAlreadyOptedIn) {
// Start with Widevine already opted in
PrefService* prefs = ProfileManager::GetActiveUserProfile()->GetPrefs();
prefs->SetBoolean(kWidevineOptedIn, true);

GURL url = embedded_test_server()->GetURL("www.netflix.com", "/blank.html");
NavigateToURLUntilLoadStop(url);

auto model = CreateModel();

Profile* profile = browser()->profile();
WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
std::unique_ptr<BraveWidevineContentSettingPluginBubbleModel> bubble(
(BraveWidevineContentSettingPluginBubbleModel*)
model->CreateBubbleModel(
browser()->content_setting_bubble_model_delegate(), web_contents,
profile));

ASSERT_FALSE(model->is_visible());
}
4 changes: 3 additions & 1 deletion common/shield_exceptions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ bool IsWidevineInstallableURL(const GURL& url) {
URLPattern(URLPattern::SCHEME_ALL, "https://bitmovin.com/*"),
URLPattern(URLPattern::SCHEME_ALL, "https://www.primevideo.com/*"),
URLPattern(URLPattern::SCHEME_ALL, "https://www.spotify.com/*"),
URLPattern(URLPattern::SCHEME_ALL, "https://shaka-player-demo.appspot.com/*")
URLPattern(URLPattern::SCHEME_ALL, "https://shaka-player-demo.appspot.com/*"),
// Used for tests
URLPattern(URLPattern::SCHEME_ALL, "http://www.netflix.com:*/*")
});
return std::any_of(patterns.begin(), patterns.end(),
[&url](URLPattern pattern){
Expand Down
44 changes: 44 additions & 0 deletions common/shield_exceptions_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/common/shield_exceptions.h"


#include "chrome/test/base/chrome_render_view_host_test_harness.h"

namespace {

typedef testing::Test BraveShieldsExceptionsTest;

TEST_F(BraveShieldsExceptionsTest, WidevineInstallableURL) {
std::vector<GURL> urls({
GURL("https://www.netflix.com/"),
GURL("https://bitmovin.com/"),
GURL("https://www.primevideo.com/"),
GURL("https://www.spotify.com/"),
GURL("https://shaka-player-demo.appspot.com"),
GURL("https://www.netflix.com/subdir"),
GURL("https://bitmovin.com/subdir"),
GURL("https://www.primevideo.com/subdir"),
GURL("https://www.spotify.com/subdir"),
GURL("https://shaka-player-demo.appspot.com/subdir")
});
std::for_each(urls.begin(), urls.end(),
[this](GURL url){
EXPECT_TRUE(brave::IsWidevineInstallableURL(url));
});
}

TEST_F(BraveShieldsExceptionsTest, NotWidevineInstallableURL) {
std::vector<GURL> urls({
GURL("https://www.brave.com/"),
GURL("https://widevine.com/")
});
std::for_each(urls.begin(), urls.end(),
[this](GURL url){
EXPECT_FALSE(brave::IsWidevineInstallableURL(url));
});
}

} // namespace
2 changes: 2 additions & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ test("brave_unit_tests") {
"//brave/chromium_src/chrome/browser/signin/account_consistency_disabled_unittest.cc",
"//brave/common/importer/brave_mock_importer_bridge.cc",
"//brave/common/importer/brave_mock_importer_bridge.h",
"//brave/common/shield_exceptions_unittest.cc",
"//chrome/common/importer/mock_importer_bridge.cc",
"//chrome/common/importer/mock_importer_bridge.h",
"../browser/importer/chrome_profile_lock_unittest.cc",
Expand Down Expand Up @@ -114,6 +115,7 @@ test("brave_browser_tests") {
"//brave/browser/brave_content_browser_client_browsertest.cc",
"//brave/browser/brave_profile_prefs_browsertest.cc",
"//brave/browser/extensions/api/brave_shields_api_browsertest.cc",
"//brave/browser/ui/content_settings/brave_widevine_blocked_image_model_browsertest.cc",
"//brave/browser/ui/webui/brave_welcome_ui_browsertest.cc",
"//brave/components/brave_shields/browser/ad_block_service_browsertest.cc",
"//brave/components/brave_shields/browser/https_everywhere_service_browsertest.cc",
Expand Down

0 comments on commit e35fcf4

Please sign in to comment.