Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add compile tests for the @SharedPref name parameter
  • Loading branch information
dodgex committed Dec 11, 2016
commit 5828a186e0098e92ac6ef6fd4bc7ca9ca750dffc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (C) 2016 the AndroidAnnotations project
*
* 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 org.androidannotations.sharedprefs;

import org.androidannotations.annotations.EActivity;

import android.app.Activity;

@EActivity
public class ActivityInManifest extends Activity {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (C) 2016 the AndroidAnnotations project
*
* 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 org.androidannotations.sharedprefs;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.sharedpreferences.SharedPref;

@EBean
public class PrefsCollection {

@SharedPref(SharedPref.Scope.ACTIVITY)
public interface ActivityPrefs {

}

@SharedPref(value = SharedPref.Scope.ACTIVITY, name = "named_pref_activity")
public interface NamedActivityPrefs {

}

@SharedPref(SharedPref.Scope.UNIQUE)
public interface UniquePrefs {

}

@SharedPref(value = SharedPref.Scope.UNIQUE, name = "named_pref_unique")
public interface NamedUniquePrefs {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (C) 2016 the AndroidAnnotations project
*
* 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 org.androidannotations.sharedprefs;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.sharedpreferences.SharedPref;

@EBean
public class PrefsCollectionWithError {

@SharedPref(value = SharedPref.Scope.ACTIVITY_DEFAULT, name = "named_pref_activity_default")
public interface NamedActivityDefaultPrefs {

}

@SharedPref(value = SharedPref.Scope.APPLICATION_DEFAULT, name = "named_pref_application_default")
public interface NamedApplicationDefaultPrefs {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright (C) 2016 the AndroidAnnotations project
*
* 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 org.androidannotations.sharedprefs;

import java.io.File;
import java.io.IOException;

import org.androidannotations.internal.AndroidAnnotationProcessor;
import org.androidannotations.testutils.AAProcessorTestHelper;
import org.junit.Before;
import org.junit.Test;

public class SharedPrefNamingTest extends AAProcessorTestHelper {

@Before
public void setUp() {
addManifestProcessorParameter(ActivityInManifest.class);
addProcessor(AndroidAnnotationProcessor.class);
ensureOutputDirectoryIsEmpty();
}

@Test
public void testNamedPrefGeneration() throws IOException {
CompileResult result = compileFiles(PrefsCollection.class);
File generatedFile = toGeneratedFile(PrefsCollection.class);

assertCompilationSuccessful(result);

// CHECKSTYLE:OFF
String[] activityPref = { //
" public ActivityPrefs_(Context context) {", //
" super(context.getSharedPreferences((getLocalClassName(context)+\"_ActivityPrefs\"), 0));", //
" }" //
};
String[] namedActivityPref = { //
" public NamedActivityPrefs_(Context context) {", //
" super(context.getSharedPreferences((getLocalClassName(context)+\"_named_pref_activity\"), 0));", //
" }" //
};
String[] uniquePref = { //
" public UniquePrefs_(Context context) {", //
" super(context.getSharedPreferences(\"UniquePrefs\", 0));", //
" }" //
};
String[] namedUniquePref = { //
" public NamedUniquePrefs_(Context context) {", //
" super(context.getSharedPreferences(\"named_pref_unique\", 0));", //
" }" //
};
// CHECKSTYLE:ON
assertGeneratedClassContains(generatedFile, activityPref);
assertGeneratedClassContains(generatedFile, namedActivityPref);
assertGeneratedClassContains(generatedFile, uniquePref);
assertGeneratedClassContains(generatedFile, namedUniquePref);
}

@Test
public void testNamedPrefGenerationErrors() throws IOException {
CompileResult result = compileFiles(PrefsCollectionWithError.class);

assertCompilationError(result);

assertCompilationErrorOn(PrefsCollectionWithError.class, "@SharedPref(value = SharedPref.Scope.ACTIVITY_DEFAULT, name = \"named_pref_activity_default\")", result);
assertCompilationErrorOn(PrefsCollectionWithError.class, "@SharedPref(value = SharedPref.Scope.APPLICATION_DEFAULT, name = \"named_pref_application_default\")", result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!--

Copyright (C) 2016 the AndroidAnnotations project

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.

-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.androidannotations.testprocessor"
android:versionCode="1"
android:versionName="1.0" >

<application>
<activity android:name="org.androidannotations.sharedprefs.ActivityInManifest_" />
</application>

</manifest>