• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 package android.platform.test.rule;
18 
19 import android.util.Log;
20 import androidx.annotation.VisibleForTesting;
21 
22 import com.google.common.collect.ImmutableList;
23 
24 import java.util.stream.Collectors;
25 
26 import org.junit.runner.Description;
27 import org.junit.runners.model.InitializationError;
28 
29 /** This rule compiles the applications with the specified filter, or skips if unspecified. */
30 public class CompilationFilterRule extends TestWatcher {
31     //
32     private static final String LOG_TAG = CompilationFilterRule.class.getSimpleName();
33     // Compilation constants
34     @VisibleForTesting static final String COMPILE_CMD_FORMAT = "cmd package compile -f -m %s %s";
35     private static final ImmutableList<String> COMPILE_FILTER_LIST =
36             ImmutableList.of("speed", "speed-profile", "quicken", "verify");
37     @VisibleForTesting static final String COMPILE_FILTER_OPTION = "compilation-filter";
38     @VisibleForTesting static final String COMPILE_SUCCESS = "Success";
39 
40     private String[] mApplications;
41 
CompilationFilterRule()42     public CompilationFilterRule() throws InitializationError {
43         throw new InitializationError("Must supply an application to compile.");
44     }
45 
CompilationFilterRule(String... applications)46     public CompilationFilterRule(String... applications) {
47         mApplications = applications;
48     }
49 
50     @Override
starting(Description description)51     protected void starting(Description description) {
52         // Identify the filter option to use.
53         String filter = getArguments().getString(COMPILE_FILTER_OPTION);
54         if (filter == null) {
55             // No option provided, default to a no-op.
56             Log.d(LOG_TAG, "Skipping complation because filter option is unset.");
57             return;
58         } else if (!COMPILE_FILTER_LIST.contains(filter)) {
59             String filterOptions = COMPILE_FILTER_LIST.stream().collect(Collectors.joining(", "));
60             throw new IllegalArgumentException(
61                     String.format(
62                             "Unknown compiler filter: %s, not part of %s", filter, filterOptions));
63         }
64         // Compile each application in sequence.
65         for (String app : mApplications) {
66             String response = executeShellCommand(String.format(COMPILE_CMD_FORMAT, filter, app));
67             if (!response.contains(COMPILE_SUCCESS)) {
68                 Log.d(LOG_TAG, String.format("Received response: %s", response));
69                 throw new RuntimeException(String.format("Failed to compile %s.", app));
70             }
71         }
72     }
73 }
74