From a0d2b2a7f02711370134f5083505a514e962a4c9 Mon Sep 17 00:00:00 2001 From: jbudorick Date: Mon, 11 May 2015 14:41:34 -0700 Subject: [PATCH] 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} --- .../device/bluetooth/BluetoothAdapter.java | 75 ++----------------- device/bluetooth/bluetooth_adapter_android.cc | 37 +++------ device/bluetooth/bluetooth_adapter_android.h | 4 - .../bluetooth_adapter_android_unittest.cc | 31 +------- .../native_test/java/AndroidManifest.xml | 3 +- 5 files changed, 19 insertions(+), 131 deletions(-) diff --git a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/BluetoothAdapter.java b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/BluetoothAdapter.java index 3c8b26521f6008..07f6349ddaaa8c 100644 --- a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/BluetoothAdapter.java +++ b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/BluetoothAdapter.java @@ -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 @@ private static BluetoothAdapter create(Context context) { } @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(); } } diff --git a/device/bluetooth/bluetooth_adapter_android.cc b/device/bluetooth/bluetooth_adapter_android.cc index 0b8bea30de4283..0d4f1408f5bddf 100644 --- a/device/bluetooth/bluetooth_adapter_android.cc +++ b/device/bluetooth/bluetooth_adapter_android.cc @@ -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::CreateAdapter( base::WeakPtr 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::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() { diff --git a/device/bluetooth/bluetooth_adapter_android.h b/device/bluetooth/bluetooth_adapter_android.h index d458bb5659cc2a..cfb9d31733b719 100644 --- a/device/bluetooth/bluetooth_adapter_android.h +++ b/device/bluetooth/bluetooth_adapter_android.h @@ -23,10 +23,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterAndroid final // Create a BluetoothAdapterAndroid instance. static base::WeakPtr CreateAdapter(); - // Create a BluetoothAdapterAndroid instance without Bluetooth permission. - static base::WeakPtr - CreateAdapterWithoutPermissionForTesting(); - // Register C++ methods exposed to Java using JNI. static bool RegisterJNI(JNIEnv* env); diff --git a/device/bluetooth/bluetooth_adapter_android_unittest.cc b/device/bluetooth/bluetooth_adapter_android_unittest.cc index 1df32a2ecf6784..c4d5252e339c7a 100644 --- a/device/bluetooth/bluetooth_adapter_android_unittest.cc +++ b/device/bluetooth/bluetooth_adapter_android_unittest.cc @@ -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 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 diff --git a/testing/android/native_test/java/AndroidManifest.xml b/testing/android/native_test/java/AndroidManifest.xml index 37cc82dc01e6cb..a92fb5fce5d5ed 100644 --- a/testing/android/native_test/java/AndroidManifest.xml +++ b/testing/android/native_test/java/AndroidManifest.xml @@ -11,8 +11,7 @@ found in the LICENSE file. android:versionName="1.0"> - - +