From 2aa3e0dd1a70c81a7d50361cb1b409dd2c3a1b16 Mon Sep 17 00:00:00 2001 From: Martin Konicek Date: Wed, 9 Dec 2015 15:16:15 -0800 Subject: [PATCH] Use the new Android Clipboard module Summary: public - Open source the unit test for `ClipboardModule`, start using the `ReactTestHelper` in two unit tests - Fixes a few references to "pasteboard" in strings Reviewed By: bestander Differential Revision: D2739614 fb-gh-sync-id: e076940a3ae5c22314e181a37fe2c3f77a18cf85 --- ...stTestHelper.java => ReactTestHelper.java} | 2 +- .../clipboard/ClipboardModuleTest.java | 61 +++++++++++++++++++ .../storage/AsyncStorageModuleTest.java | 12 +--- 3 files changed, 64 insertions(+), 11 deletions(-) rename ReactAndroid/src/test/java/com/facebook/react/bridge/{CatalystTestHelper.java => ReactTestHelper.java} (98%) create mode 100644 ReactAndroid/src/test/java/com/facebook/react/modules/clipboard/ClipboardModuleTest.java diff --git a/ReactAndroid/src/test/java/com/facebook/react/bridge/CatalystTestHelper.java b/ReactAndroid/src/test/java/com/facebook/react/bridge/ReactTestHelper.java similarity index 98% rename from ReactAndroid/src/test/java/com/facebook/react/bridge/CatalystTestHelper.java rename to ReactAndroid/src/test/java/com/facebook/react/bridge/ReactTestHelper.java index 27a1b7a9f..0aa236ddb 100644 --- a/ReactAndroid/src/test/java/com/facebook/react/bridge/CatalystTestHelper.java +++ b/ReactAndroid/src/test/java/com/facebook/react/bridge/ReactTestHelper.java @@ -24,7 +24,7 @@ import static org.mockito.Mockito.when; /** * Utility for creating pre-configured instances of core react components for tests. */ -public class CatalystTestHelper { +public class ReactTestHelper { /** * @return a ReactApplicationContext that has a CatalystInstance mock returned by diff --git a/ReactAndroid/src/test/java/com/facebook/react/modules/clipboard/ClipboardModuleTest.java b/ReactAndroid/src/test/java/com/facebook/react/modules/clipboard/ClipboardModuleTest.java new file mode 100644 index 000000000..0486ae79f --- /dev/null +++ b/ReactAndroid/src/test/java/com/facebook/react/modules/clipboard/ClipboardModuleTest.java @@ -0,0 +1,61 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.react.modules.clipboard; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.text.ClipboardManager; + +import com.facebook.react.bridge.ReactTestHelper; +import com.facebook.react.modules.clipboard.ClipboardModule; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; + +@SuppressLint({"ClipboardManager", "DeprecatedClass"}) +@RunWith(RobolectricTestRunner.class) +@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"}) +public class ClipboardModuleTest { + + private static final String TEST_CONTENT = "test"; + + private ClipboardModule mClipboardModule; + private ClipboardManager mClipboardManager; + + @Before + public void setUp() { + mClipboardModule = new ClipboardModule(ReactTestHelper.createCatalystContextForTest()); + mClipboardManager = + (ClipboardManager) RuntimeEnvironment.application.getSystemService(Context.CLIPBOARD_SERVICE); + } + + @Test + public void testSetString() { + mClipboardModule.setString(TEST_CONTENT); + assertTrue(mClipboardManager.getText().equals(TEST_CONTENT)); + + mClipboardModule.setString(null); + assertFalse(mClipboardManager.hasText()); + + mClipboardModule.setString(""); + assertFalse(mClipboardManager.hasText()); + + mClipboardModule.setString(" "); + assertTrue(mClipboardManager.hasText()); + } +} diff --git a/ReactAndroid/src/test/java/com/facebook/react/modules/storage/AsyncStorageModuleTest.java b/ReactAndroid/src/test/java/com/facebook/react/modules/storage/AsyncStorageModuleTest.java index c11e9ba93..819aececc 100644 --- a/ReactAndroid/src/test/java/com/facebook/react/modules/storage/AsyncStorageModuleTest.java +++ b/ReactAndroid/src/test/java/com/facebook/react/modules/storage/AsyncStorageModuleTest.java @@ -20,12 +20,12 @@ import android.content.ContextWrapper; import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactTestHelper; import com.facebook.react.bridge.SimpleArray; import com.facebook.react.bridge.SimpleMap; import com.facebook.react.modules.storage.AsyncStorageModule; import com.facebook.react.modules.storage.ReactDatabaseSupplier; - import org.json.JSONArray; import org.json.JSONObject; import org.junit.After; @@ -60,13 +60,6 @@ public class AsyncStorageModuleTest { private AsyncStorageModule mStorage; private SimpleArray mEmptyArray; - private static class FakeFragmentContext extends ContextWrapper { - - public FakeFragmentContext(Context base) { - super(base); - } - } - @Rule public PowerMockRule rule = new PowerMockRule(); @@ -90,8 +83,7 @@ public class AsyncStorageModuleTest { }); // don't use Robolectric before initializing mocks - mStorage = new AsyncStorageModule(new ReactApplicationContext( - new FakeFragmentContext(RuntimeEnvironment.application))); + mStorage = new AsyncStorageModule(ReactTestHelper.createCatalystContextForTest()); mEmptyArray = new SimpleArray(); }