Commit a0d2b2a7 authored by jbudorick's avatar jbudorick Committed by Commit bot

Revert of bluetooth: Android adapter can be created with and without Bluetooth...

Revert of bluetooth: Android adapter can be created with and without Bluetooth permission. (patchset #2 id:110001 of https://codereview.chromium.org/1129683002/)

Reason for revert:
Failing on the main waterfall: http://build.chromium.org/p/chromium.linux/builders/Android%20Tests%20%28dbg%29/builds/27824

Original issue's description:
> bluetooth: Android adapter can be created with and without Bluetooth permission.
>
> This enables unit tests to run with Bluetooth permission,
> by adding the Bluetooth permission to native_test.
>
> Non-test applications will not have the Bluetooth permission,
> and unit tests should also verify behavior
> when the permission is not given. To enable this
> createWithoutPermissionForTesting is added and
> results in a state equivalent to when the permission
> is not available.
>
> Unit tests will be built in parallel for both when Bluetooth
> permission exists and doesn't.
>
> BUG=471536
>
> Committed: https://crrev.com/30e81472dc168ea1c331a8779b3f6a14f684c54b
> Cr-Commit-Position: refs/heads/master@{#329212}

TBR=tedchoc@chromium.org,armansito@chromium.org,scheib@chromium.org
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=471536

Review URL: https://codereview.chromium.org/1137083002

Cr-Commit-Position: refs/heads/master@{#329240}
parent e86f8379
......@@ -4,9 +4,7 @@
package org.chromium.device.bluetooth;
import android.Manifest;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.PackageManager;
import org.chromium.base.CalledByNative;
......@@ -22,7 +20,6 @@ final class BluetoothAdapter {
private static final String TAG = Log.makeTag("Bluetooth");
private final boolean mHasBluetoothPermission;
private android.bluetooth.BluetoothAdapter mAdapter;
@CalledByNative
private static BluetoothAdapter create(Context context) {
......@@ -30,78 +27,16 @@ final class BluetoothAdapter {
}
@CalledByNative
private static BluetoothAdapter createWithoutPermissionForTesting(Context context) {
Context contextWithoutPermission = new ContextWrapper(context) {
@Override
public int checkCallingOrSelfPermission(String permission) {
return PackageManager.PERMISSION_DENIED;
}
};
return new BluetoothAdapter(contextWithoutPermission);
private boolean hasBluetoothPermission() {
return mHasBluetoothPermission;
}
// Constructs a BluetoothAdapter.
private BluetoothAdapter(Context context) {
mHasBluetoothPermission =
context.checkCallingOrSelfPermission(Manifest.permission.BLUETOOTH)
== PackageManager.PERMISSION_GRANTED
&& context.checkCallingOrSelfPermission(Manifest.permission.BLUETOOTH_ADMIN)
== PackageManager.PERMISSION_GRANTED;
context.checkCallingOrSelfPermission(android.Manifest.permission.BLUETOOTH)
== PackageManager.PERMISSION_GRANTED;
if (!mHasBluetoothPermission) {
Log.w(TAG,
"Bluetooth API disabled; BLUETOOTH and BLUETOOTH_ADMIN permissions required.");
return;
Log.w(TAG, "Can not use bluetooth API, requires BLUETOOTH permission.");
}
mAdapter = android.bluetooth.BluetoothAdapter.getDefaultAdapter();
if (mAdapter == null) Log.i(TAG, "No adapter found.");
}
@CalledByNative
private boolean hasBluetoothPermission() {
return mHasBluetoothPermission;
}
// ---------------------------------------------------------------------------------------------
// BluetoothAdapterAndroid.h interface:
@CalledByNative
private String getAddress() {
if (isPresent()) {
return mAdapter.getAddress();
} else {
return "";
}
}
@CalledByNative
private String getName() {
if (isPresent()) {
return mAdapter.getName();
} else {
return "";
}
}
@CalledByNative
private boolean isPresent() {
return mAdapter != null;
}
@CalledByNative
private boolean isPowered() {
return isPresent() && mAdapter.isEnabled();
}
@CalledByNative
private boolean isDiscoverable() {
return isPresent()
&& mAdapter.getScanMode()
== android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE;
}
@CalledByNative
private boolean isDiscovering() {
return isPresent() && mAdapter.isDiscovering();
}
}
......@@ -5,7 +5,6 @@
#include "device/bluetooth/bluetooth_adapter_android.h"
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/sequenced_task_runner.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
......@@ -13,7 +12,6 @@
#include "jni/BluetoothAdapter_jni.h"
using base::android::AttachCurrentThread;
using base::android::ConvertJavaStringToUTF8;
namespace device {
......@@ -27,17 +25,6 @@ base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter(
base::WeakPtr<BluetoothAdapterAndroid>
BluetoothAdapterAndroid::CreateAdapter() {
BluetoothAdapterAndroid* adapter = new BluetoothAdapterAndroid();
adapter->j_bluetooth_adapter_.Reset(Java_BluetoothAdapter_create(
AttachCurrentThread(), base::android::GetApplicationContext()));
return adapter->weak_ptr_factory_.GetWeakPtr();
}
base::WeakPtr<BluetoothAdapterAndroid>
BluetoothAdapterAndroid::CreateAdapterWithoutPermissionForTesting() {
BluetoothAdapterAndroid* adapter = new BluetoothAdapterAndroid();
adapter->j_bluetooth_adapter_.Reset(
Java_BluetoothAdapter_createWithoutPermissionForTesting(
AttachCurrentThread(), base::android::GetApplicationContext()));
return adapter->weak_ptr_factory_.GetWeakPtr();
}
......@@ -52,13 +39,11 @@ bool BluetoothAdapterAndroid::HasBluetoothPermission() const {
}
std::string BluetoothAdapterAndroid::GetAddress() const {
return ConvertJavaStringToUTF8(Java_BluetoothAdapter_getAddress(
AttachCurrentThread(), j_bluetooth_adapter_.obj()));
return address_;
}
std::string BluetoothAdapterAndroid::GetName() const {
return ConvertJavaStringToUTF8(Java_BluetoothAdapter_getName(
AttachCurrentThread(), j_bluetooth_adapter_.obj()));
return name_;
}
void BluetoothAdapterAndroid::SetName(const std::string& name,
......@@ -73,13 +58,13 @@ bool BluetoothAdapterAndroid::IsInitialized() const {
}
bool BluetoothAdapterAndroid::IsPresent() const {
return Java_BluetoothAdapter_isPresent(AttachCurrentThread(),
j_bluetooth_adapter_.obj());
NOTIMPLEMENTED();
return false;
}
bool BluetoothAdapterAndroid::IsPowered() const {
return Java_BluetoothAdapter_isPowered(AttachCurrentThread(),
j_bluetooth_adapter_.obj());
NOTIMPLEMENTED();
return false;
}
void BluetoothAdapterAndroid::SetPowered(bool powered,
......@@ -89,8 +74,8 @@ void BluetoothAdapterAndroid::SetPowered(bool powered,
}
bool BluetoothAdapterAndroid::IsDiscoverable() const {
return Java_BluetoothAdapter_isDiscoverable(AttachCurrentThread(),
j_bluetooth_adapter_.obj());
NOTIMPLEMENTED();
return false;
}
void BluetoothAdapterAndroid::SetDiscoverable(
......@@ -101,8 +86,8 @@ void BluetoothAdapterAndroid::SetDiscoverable(
}
bool BluetoothAdapterAndroid::IsDiscovering() const {
return Java_BluetoothAdapter_isDiscovering(AttachCurrentThread(),
j_bluetooth_adapter_.obj());
NOTIMPLEMENTED();
return false;
}
void BluetoothAdapterAndroid::CreateRfcommService(
......@@ -138,6 +123,8 @@ void BluetoothAdapterAndroid::RegisterAdvertisement(
}
BluetoothAdapterAndroid::BluetoothAdapterAndroid() : weak_ptr_factory_(this) {
j_bluetooth_adapter_.Reset(Java_BluetoothAdapter_create(
AttachCurrentThread(), base::android::GetApplicationContext()));
}
BluetoothAdapterAndroid::~BluetoothAdapterAndroid() {
......
......@@ -23,10 +23,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterAndroid final
// Create a BluetoothAdapterAndroid instance.
static base::WeakPtr<BluetoothAdapterAndroid> CreateAdapter();
// Create a BluetoothAdapterAndroid instance without Bluetooth permission.
static base::WeakPtr<BluetoothAdapterAndroid>
CreateAdapterWithoutPermissionForTesting();
// Register C++ methods exposed to Java using JNI.
static bool RegisterJNI(JNIEnv* env);
......
......@@ -10,45 +10,16 @@ namespace device {
class BluetoothAdapterAndroidTest : public testing::Test {
protected:
void InitWithPermission() {
BluetoothAdapterAndroidTest() {
adapter_ = BluetoothAdapterAndroid::CreateAdapter().get();
}
void InitWithoutPermission() {
adapter_ =
BluetoothAdapterAndroid::CreateAdapterWithoutPermissionForTesting()
.get();
}
scoped_refptr<BluetoothAdapterAndroid> adapter_;
};
TEST_F(BluetoothAdapterAndroidTest, Construct) {
InitWithPermission();
ASSERT_TRUE(adapter_.get());
EXPECT_TRUE(adapter_->HasBluetoothPermission());
if (!adapter_->IsPresent()) {
LOG(WARNING) << "Bluetooth adapter not present; skipping unit test.";
return;
}
EXPECT_GT(adapter_->GetAddress().length(), 0u);
EXPECT_GT(adapter_->GetName().length(), 0u);
EXPECT_TRUE(adapter_->IsPresent());
EXPECT_TRUE(adapter_->IsPowered());
EXPECT_FALSE(adapter_->IsDiscoverable());
EXPECT_FALSE(adapter_->IsDiscovering());
}
TEST_F(BluetoothAdapterAndroidTest, ConstructNoPermision) {
InitWithoutPermission();
ASSERT_TRUE(adapter_.get());
EXPECT_FALSE(adapter_->HasBluetoothPermission());
EXPECT_EQ(adapter_->GetAddress().length(), 0u);
EXPECT_EQ(adapter_->GetName().length(), 0u);
EXPECT_FALSE(adapter_->IsPresent());
EXPECT_FALSE(adapter_->IsPowered());
EXPECT_FALSE(adapter_->IsDiscoverable());
EXPECT_FALSE(adapter_->IsDiscovering());
}
} // namespace device
......@@ -11,8 +11,7 @@ found in the LICENSE file.
android:versionName="1.0">
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment