1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 package com.android.settings.search.indexing; 19 20 import static com.google.common.truth.Truth.assertThat; 21 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.Intent; 25 26 import com.android.settings.search.InlineSwitchPayload; 27 import com.android.settings.search.ResultPayload; 28 import com.android.settings.search.ResultPayloadUtils; 29 import com.android.settings.testutils.SettingsRobolectricTestRunner; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.robolectric.RuntimeEnvironment; 35 36 @RunWith(SettingsRobolectricTestRunner.class) 37 public class IndexDataTest { 38 39 private IndexData.Builder mBuilder; 40 41 private static final String LOCALE = "en_US"; 42 private static final String TITLE = "updated-title"; 43 private static final String NORM_TITLE = "updatedtitle"; 44 private static final String SUMMARY_ON = "updated-summary-on"; 45 private static final String NORM_SUMMARY_ON = "updatedsummaryon"; 46 private static final String SUMMARY_OFF = "updated-summary-off"; 47 private static final String NORM_SUMMARY_OFF = "updatedsummaryoff"; 48 private static final String ENTRIES = "entries"; 49 private static final String CLASS_NAME = "class name"; 50 private static final String SCREEN_TITLE = "screen title"; 51 private static final int ICON_RES_ID = 0xff; 52 private static final String SPACE_DELIMITED_KEYWORDS = "keywords"; 53 private static final String INTENT_ACTION = "intent action"; 54 private static final String INTENT_TARGET_PACKAGE = "target package"; 55 private static final String INTENT_TARGET_CLASS = "target class"; 56 private static final boolean ENABLED = true; 57 private static final String KEY = "key"; 58 private static final int USER_ID = 1; 59 60 private Context mContext; 61 62 @Before 63 public void setUp() { 64 mContext = RuntimeEnvironment.application; 65 mBuilder = createBuilder(); 66 } 67 68 @Test 69 public void testFullRowBuild_nonNull() { 70 IndexData row = generateRow(); 71 assertThat(row).isNotNull(); 72 } 73 74 @Test 75 public void testPrimitivesBuild_noDataLoss() { 76 IndexData row = generateRow(); 77 78 assertThat(row.locale).isEqualTo(LOCALE); 79 assertThat(row.updatedTitle).isEqualTo(TITLE); 80 assertThat(row.normalizedTitle).isEqualTo(NORM_TITLE); 81 assertThat(row.updatedSummaryOn).isEqualTo(SUMMARY_ON); 82 assertThat(row.normalizedSummaryOn).isEqualTo(NORM_SUMMARY_ON); 83 assertThat(row.entries).isEqualTo(ENTRIES); 84 assertThat(row.className).isEqualTo(CLASS_NAME); 85 assertThat(row.screenTitle).isEqualTo(SCREEN_TITLE); 86 assertThat(row.iconResId).isEqualTo(ICON_RES_ID); 87 assertThat(row.spaceDelimitedKeywords).isEqualTo(SPACE_DELIMITED_KEYWORDS); 88 assertThat(row.intentAction).isEqualTo(INTENT_ACTION); 89 assertThat(row.intentTargetClass).isEqualTo(INTENT_TARGET_CLASS); 90 assertThat(row.intentTargetPackage).isEqualTo(INTENT_TARGET_PACKAGE); 91 assertThat(row.enabled).isEqualTo(ENABLED); 92 assertThat(row.userId).isEqualTo(USER_ID); 93 assertThat(row.key).isEqualTo(KEY); 94 assertThat(row.payloadType).isEqualTo(ResultPayload.PayloadType.INTENT); 95 assertThat(row.payload).isNotNull(); 96 } 97 98 @Test 99 public void testGenericIntent_addedToPayload() { 100 final IndexData row = generateRow(); 101 final ResultPayload payload = 102 ResultPayloadUtils.unmarshall(row.payload, ResultPayload.CREATOR); 103 final ComponentName name = payload.getIntent().getComponent(); 104 assertThat(name.getClassName()).isEqualTo(INTENT_TARGET_CLASS); 105 assertThat(name.getPackageName()).isEqualTo(INTENT_TARGET_PACKAGE); 106 } 107 108 @Test 109 public void testRowWithInlinePayload_genericPayloadNotAdded() { 110 final String URI = "test uri"; 111 final InlineSwitchPayload payload = new InlineSwitchPayload(URI, 0 /* mSettingSource */, 112 1 /* onValue */, null /* intent */, true /* isDeviceSupported */, 1 /* default */); 113 mBuilder.setPayload(payload); 114 final IndexData row = generateRow(); 115 final InlineSwitchPayload unmarshalledPayload = 116 ResultPayloadUtils.unmarshall(row.payload, InlineSwitchPayload.CREATOR); 117 118 assertThat(row.payloadType).isEqualTo(ResultPayload.PayloadType.INLINE_SWITCH); 119 assertThat(unmarshalledPayload.getKey()).isEqualTo(URI); 120 } 121 122 @Test 123 public void testRowWithInlinePayload_intentAddedToInlinePayload() { 124 final String URI = "test uri"; 125 final ComponentName component = 126 new ComponentName(INTENT_TARGET_PACKAGE, INTENT_TARGET_CLASS); 127 final Intent intent = new Intent(); 128 intent.setComponent(component); 129 130 final InlineSwitchPayload payload = new InlineSwitchPayload(URI, 0 /* mSettingSource */, 131 1 /* onValue */, intent, true /* isDeviceSupported */, 1 /* default */); 132 mBuilder.setPayload(payload); 133 final IndexData row = generateRow(); 134 final InlineSwitchPayload unmarshalledPayload = ResultPayloadUtils 135 .unmarshall(row.payload, InlineSwitchPayload.CREATOR); 136 final ComponentName name = unmarshalledPayload.getIntent().getComponent(); 137 138 assertThat(name.getClassName()).isEqualTo(INTENT_TARGET_CLASS); 139 assertThat(name.getPackageName()).isEqualTo(INTENT_TARGET_PACKAGE); 140 } 141 142 @Test 143 public void testNormalizeJapaneseString() { 144 final String japaneseString = "\u3042\u3077\u308a"; 145 final String normalizedJapaneseString = "\u30a2\u30d5\u309a\u30ea"; 146 147 String result = IndexData.normalizeJapaneseString(japaneseString); 148 assertThat(result).isEqualTo(normalizedJapaneseString); 149 } 150 151 private IndexData generateRow() { 152 return mBuilder.build(mContext); 153 } 154 155 private IndexData.Builder createBuilder() { 156 mBuilder = new IndexData.Builder(); 157 mBuilder.setTitle(TITLE) 158 .setSummaryOn(SUMMARY_ON) 159 .setEntries(ENTRIES) 160 .setClassName(CLASS_NAME) 161 .setScreenTitle(SCREEN_TITLE) 162 .setIconResId(ICON_RES_ID) 163 .setKeywords(SPACE_DELIMITED_KEYWORDS) 164 .setIntentAction(INTENT_ACTION) 165 .setIntentTargetPackage(INTENT_TARGET_PACKAGE) 166 .setIntentTargetClass(INTENT_TARGET_CLASS) 167 .setEnabled(ENABLED) 168 .setKey(KEY) 169 .setUserId(USER_ID); 170 return mBuilder; 171 } 172 }