diff options
Diffstat (limited to 'livetests/com/google')
9 files changed, 228 insertions, 0 deletions
diff --git a/livetests/com/google/android/testing/mocking/test/AndroidManifest.xml b/livetests/com/google/android/testing/mocking/test/AndroidManifest.xml new file mode 100644 index 0000000..a35c31e --- /dev/null +++ b/livetests/com/google/android/testing/mocking/test/AndroidManifest.xml @@ -0,0 +1,30 @@ +<!-- +/* + * Copyright 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +--> +<?xml version="1.0" encoding="utf-8"?> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.google.android.testing.mocking.test" + android:versionCode="1" + android:versionName="1.0"> + <application android:label="@string/app_name"> + <uses-library android:name="android.test.runner" /> + </application> + <uses-sdk android:minSdkVersion="3"/> + <instrumentation android:targetPackage="com.google.android.testing.mocking.test" + android:name="android.test.InstrumentationTestRunner" /> +</manifest> + diff --git a/livetests/com/google/android/testing/mocking/test/MockingTest.java b/livetests/com/google/android/testing/mocking/test/MockingTest.java new file mode 100644 index 0000000..ee527a9 --- /dev/null +++ b/livetests/com/google/android/testing/mocking/test/MockingTest.java @@ -0,0 +1,62 @@ +/* + * Copyright 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.android.testing.mocking.test; + +import android.content.Context; + +import com.google.android.testing.mocking.AndroidMock; +import com.google.android.testing.mocking.SdkVersion; +import com.google.android.testing.mocking.UsesMocks; +import com.google.android.testing.mocking.testapp.ClassToMock; + +import junit.framework.TestCase; + +/** + * Tests that Android Mock provides correct mocks when running on a Dalvik VM. + * + * @author swoodward (Stephen Woodward) + */ +public class MockingTest extends TestCase { + /** + * Test that an SDK class is mocked correctly, that is to say the mock + * comes from the pre-generated set, and it corresponds to the correct + * runtime environment + */ + @UsesMocks(Context.class) + public void testFrameworkMock() { + Context mockContext = AndroidMock.createMock(Context.class); + String packageName = mockContext.getClass().getPackage().getName(); + assertEquals(SdkVersion.getCurrentVersion().getPackagePrefix(), + packageName.substring(0, packageName.indexOf('.') + 1)); + } + + /** + * Test that a non-SDK class is mocked correctly + */ + @UsesMocks(ClassToMock.class) + public void testNormalMock() { + ClassToMock myMockClass = AndroidMock.createMock(ClassToMock.class); + AndroidMock.expect(myMockClass.getString()).andReturn( + "I'm the king of the world, king of the -- d'oh!"); + AndroidMock.expect(myMockClass.getInt()).andReturn(42); + AndroidMock.replay(myMockClass); + assertEquals("I'm the king of the world, king of the -- d'oh!", + myMockClass.getString()); + assertEquals(42, myMockClass.getInt()); + AndroidMock.verify(myMockClass); + } +} + diff --git a/livetests/com/google/android/testing/mocking/test/res/layout/main.xml b/livetests/com/google/android/testing/mocking/test/res/layout/main.xml new file mode 100644 index 0000000..c1fe411 --- /dev/null +++ b/livetests/com/google/android/testing/mocking/test/res/layout/main.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + > +<TextView + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:text="@string/hello" + /> +</LinearLayout> + diff --git a/livetests/com/google/android/testing/mocking/test/res/values/strings.xml b/livetests/com/google/android/testing/mocking/test/res/values/strings.xml new file mode 100644 index 0000000..224fc4a --- /dev/null +++ b/livetests/com/google/android/testing/mocking/test/res/values/strings.xml @@ -0,0 +1,5 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string name="hello">Hello World!</string> + <string name="app_name">AndroidMockTestAppTest</string> +</resources> diff --git a/livetests/com/google/android/testing/mocking/testapp/AndroidManifest.xml b/livetests/com/google/android/testing/mocking/testapp/AndroidManifest.xml new file mode 100644 index 0000000..02fdb20 --- /dev/null +++ b/livetests/com/google/android/testing/mocking/testapp/AndroidManifest.xml @@ -0,0 +1,33 @@ +<!-- +/* + * Copyright 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +--> +<?xml version="1.0" encoding="utf-8"?> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.google.android.testing.mocking.testapp" + android:versionCode="1" + android:versionName="1.0"> + <application android:label="@string/app_name"> + <activity android:name=".TestAppActivity" + android:label="@string/app_name"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> + </application> + <uses-sdk android:minSdkVersion="3" /> +</manifest> diff --git a/livetests/com/google/android/testing/mocking/testapp/ClassToMock.java b/livetests/com/google/android/testing/mocking/testapp/ClassToMock.java new file mode 100644 index 0000000..8c51220 --- /dev/null +++ b/livetests/com/google/android/testing/mocking/testapp/ClassToMock.java @@ -0,0 +1,31 @@ +/* + * Copyright 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.android.testing.mocking.testapp; + +/** + * Dummy class to be mocked. + * + * @author swoodward@google.com (Stephen Woodward) + */ +public class ClassToMock { + public String getString() { + return "foo"; + } + + public int getInt() { + return -1; + } +} diff --git a/livetests/com/google/android/testing/mocking/testapp/TestAppActivity.java b/livetests/com/google/android/testing/mocking/testapp/TestAppActivity.java new file mode 100644 index 0000000..c1d6756 --- /dev/null +++ b/livetests/com/google/android/testing/mocking/testapp/TestAppActivity.java @@ -0,0 +1,36 @@ +/* + * Copyright 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.android.testing.mocking.testapp; + +import com.google.android.testing.mocking.testapp.R; +import com.google.android.testing.mocking.testapp.R.layout; + +import android.app.Activity; +import android.os.Bundle; + +/** + * Extremely simple Activity to give our Mocking Test something to test. + * + * @author swoodward@google.com (Stephen Woodward) + */ +public class TestAppActivity extends Activity { + /** Called when the activity is first created. */ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.main); + } +} diff --git a/livetests/com/google/android/testing/mocking/testapp/res/layout/main.xml b/livetests/com/google/android/testing/mocking/testapp/res/layout/main.xml new file mode 100644 index 0000000..c1fe411 --- /dev/null +++ b/livetests/com/google/android/testing/mocking/testapp/res/layout/main.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + > +<TextView + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:text="@string/hello" + /> +</LinearLayout> + diff --git a/livetests/com/google/android/testing/mocking/testapp/res/values/strings.xml b/livetests/com/google/android/testing/mocking/testapp/res/values/strings.xml new file mode 100644 index 0000000..e53d336 --- /dev/null +++ b/livetests/com/google/android/testing/mocking/testapp/res/values/strings.xml @@ -0,0 +1,5 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string name="hello">Hello World, TestAppActivity!</string> + <string name="app_name">AndroidMockTestApp</string> +</resources> |